Hibernate,获取uuid的映射异常

Hibernate, getting mapping exception for uuid

我有一个简单的 main class,没有实体对象,普通的 SQL 需要 UUID 作为输出。我得到一个映射异常,为了解决映射异常,我创建了自己的自定义方言,即使映射异常仍然存在。

hibernate 版本很旧,3.2.6,由于一些限制,我无法升级。让我知道我是否仍然可以解决方法。

我的函数如下所示:

SQLQuery getGidQuery = session.createSQLQuery("SELECT gid FROM table WHERE id = " + result.getId());
UUID gid = UUID.fromString(getGidQuery.uniqueResult().toString());

我得到以下异常

Exception in thread "main" org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
at org.hibernate.dialect.TypeNames.get(TypeNames.java:56)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:81)
at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:370)
at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:559)
at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:485)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:501)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1796)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:811)
at Main.main(Main.java:44)

为了避免上述异常,我创建了自己的方言 class 扩展了 PostgresDialect,其中包括 UUID 方言,但仍然抛出异常。

public class PostgresUuidDialect extends PostgreSQLDialect {

public PostgresUuidDialect() {
    super();
    registerColumnType(java.sql.Types.OTHER, "pg-uuid");
}


}

我的配置创建

private static Configuration getDatabaseConfiguration(CommandLine commandLine, String database) {
    Configuration configuration = new AnnotationConfiguration();
    configuration.setProperty("hibernate.connection.username", commandLine.getOptionValue('u'));
    configuration.setProperty("hibernate.connection.password", commandLine.getOptionValue('p'));
    configuration.setProperty("hibernate.connection.url",
            "jdbc:postgresql://" + commandLine.getOptionValue('h') + ":" + commandLine.getOptionValue("o")
                    + "/" + database + "?searchpath=" + database);
    configuration.setProperty("hibernate.dialect", "<package>.PostgresUuidDialect");
    return configuration;
}

尝试以下一些方法:

.addScalar("ID", StringType.INSTANCE)

(使用正确的类型,不一定是 StringType)

RegisterHibernateType( ... )

在你的构造函数中

source