Search code examples
javapostgresqlhibernateuuid

Hibernate, getting mapping exception for uuid


I have a simple main class with no entity objects, plain SQL needing UUID as output. I get a mapping exception, to work around the mapping exception, I created my own custom dialect, even then the mapping exception still exists.

The hibernate version is very old, 3.2.6 and I am not able to upgrade because of few constraints. Let me know if I can still workaround.

My function looks like this:

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

I get the below exception

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)

To avoid the above exception,I created my own dialect class extending the PostgresDialect, which includes the UUID dialect but the exception is still thrown.

public class PostgresUuidDialect extends PostgreSQLDialect {

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


}

My configuration creation

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;
}

Solution

  • Try some of the following:

    .addScalar("ID", StringType.INSTANCE)
    

    (use the proper type, not necessarily StringType)

    RegisterHibernateType( ... )
    

    in your constructor

    source