休眠,获取uuid的映射异常

时间:2018-06-24 02:46:55

标签: java postgresql hibernate uuid

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

休眠版本非常旧,为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)

为避免上述异常,我创建了自己的方言类,扩展了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;
}

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

.addScalar("ID", StringType.INSTANCE)

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

RegisterHibernateType( ... )

在您的构造函数中

source

相关问题