Hibernate Tools failing to get index info from Pos

2019-07-30 22:34发布

问题:

I'm using Hibernate Tools with Ant to generate POJOs from a Postgres database.

My database has two simple tables: Person and Vehicle. Each table has two columns: an id and a name

When the Person POJO is being generated, Hibernate throws an exception when attempting to obtain index info:

[hibernatetool] getIndexInfo(null.public.person)
[hibernatetool] Exception while trying to get indexinfo on public.person=Exception while getting index info for public.person

Despite this exception the Person POJO is created successfully, and Ant build then continues to generate the Vehicle POJO, but fails. I believe it's because a transaction remained open because of the aforementioned exception above.

Here is the full log:

[hibernatetool] Created database namespace [logicalName=Name{catalog=null, schema=null}, physicalName=Name{catalog=null, schema=null}]
[hibernatetool] Created database namespace [logicalName=Name{catalog=null, schema=null}, physicalName=Name{catalog=null, schema=null}]
[hibernatetool] getTables(null.null.null)
[hibernatetool] Adding table person of type TABLE
[hibernatetool] Normalizing identifier quoting [public]
[hibernatetool] Created database namespace [logicalName=Name{catalog=null, schema=public}, physicalName=Name{catalog=null, schema=public}]
[hibernatetool] Normalizing identifier quoting [person]
[hibernatetool] Adding table vehicle of type TABLE
[hibernatetool] Normalizing identifier quoting [public]
[hibernatetool] Normalizing identifier quoting [vehicle]
[hibernatetool] Finding columns for public.person
[hibernatetool] getColumns(null.public.person.null)
[hibernatetool] getPrimaryKeys(null.public.person)
[hibernatetool] Forcing column [id] to be non-null as it is part of the primary key for table [person]
[hibernatetool] primary key for org.hibernate.mapping.Table(public.person) -> org.hibernate.mapping.PrimaryKey(person[org.hibernate.mapping.Column(id)]) as personpkey
[hibernatetool] getIndexInfo(null.public.person)
[hibernatetool] Exception while trying to get indexinfo on public.person=Exception while getting index info for public.person
[hibernatetool] Finding columns for public.vehicle
[hibernatetool] getColumns(null.public.vehicle.null)
[hibernatetool] An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] org.hibernate.exception.GenericJDBCException: Error while reading column meta data for public.vehicle
[hibernatetool] org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block

BUILD FAILED
build.xml:97: org.hibernate.exception.GenericJDBCException: Error while reading column meta data for public.vehicle
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect.getColumns(JDBCMetaDataDialect.java:121)
    ... <many more>
Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:645)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:481)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:361)
    at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getColumns(AbstractJdbc2DatabaseMetaData.java:2460)
    at org.postgresql.jdbc4.AbstractJdbc4DatabaseMetaData.getColumns(AbstractJdbc4DatabaseMetaData.java:102)
    at org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect.getColumns(JDBCMetaDataDialect.java:99)
    ... <many more>

Total time: 4 seconds

I believe if I can resolve the getIndexInfo issue then the GenericJDBCException wouldn't be happening. I'm hoping someone here can help me figure out how to do this.

Here are my config and build files:

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/thedb</property>
        <property name="hibernate.connection_pool_size">1</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    </session-factory>
</hibernate-configuration>

build.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE project>
<project basedir="." name="HibernateTest">
    <property environment="env" />
    <property name="target" value="1.6" />
    <property name="source" value="1.6" />
    <property name="conf.dir" value="conf" />
    <property name="build.dir" value="src" />
    <path id="properties">
        <dirset dir="${conf.dir}"/>
    </path>
    <path id="toolslib">
        <pathelement location="conf" />
        <pathelement location="lib" />
    </path>
    <taskdef name="hibernatetool" 
      classname="org.hibernate.tool.ant.HibernateToolTask" 
      classpathref="toolslib" />

    <hibernatetool destdir="${build.dir}/generated">
        <jdbcconfiguration 
            packagename="com.test" 
            configurationfile="${conf.dir}/hibernate.cfg.xml" 
            revengfile="${conf.dir}/hibernate.reveng.xml" 
            detectmanytomany="true" />

        <hbm2java ejb3="true" />
    </hibernatetool>
</project>

And here is what my Postgres tables look like:

                                          Table "public.person"
 Column |         Type          | Collation | Nullable | Default | Storage  | Stats target | Description 
--------+-----------------------+-----------+----------+---------+----------+--------------+-------------
 id     | integer               |           | not null |         | plain    |              | 
 name   | character varying(80) |           |          |         | extended |              | 
Indexes:
    "personpkey" PRIMARY KEY, btree (id)

                                         Table "public.vehicle"
 Column |         Type          | Collation | Nullable | Default | Storage  | Stats target | Description 
--------+-----------------------+-----------+----------+---------+----------+--------------+-------------
 id     | integer               |           | not null |         | plain    |              | 
 name   | character varying(80) |           |          |         | extended |              | 
Indexes:
    "vehiclepkey" PRIMARY KEY, btree (id)

So in summary: Why is hibernate tools hitting an exception while trying to get index info from my Postgres tables?

回答1:

Figured it out. Turns out I was using an outdated PostgreSQL JDBC driver that wasn't compatible with my PostgreSQL 10 database server. Replaced it with the correct jar file and it's working now.