Discriminator value used as ID when inserting entity to PosgtreSQL


When we have inheritance strategy SINGLE_TABLE, and we use discriminator column, this column is the first one in table. Now we want to insert some entity inheriting from this one, and we want PostgreSQL to automatically generate the ID for the new entity. Discriminator column is the first in the table.

Hibernate runs IdentityGenerator.GetGeneratedKeysDelegate.executeAndExtract(), which gets ResultSet from PreparedStatement (in Hibernate 3.6.0 it's line 97 in IdentityGenerator).

This ResultSet, in case of PostgreSQL, contains whole inserted row, not only ID. This is the problem if ID is not the first column, because then Hibernate get's value from the first column and uses it as ID. It's in IdentifierGeneratorHelper.getGeneratedIdentity() and IdentifierGeneratorHelper.get().


Tested on Hibernate 3.6.0, but I looked in the code of 3.6.6 and 4.0.0 Beta4 and it looks like it isn't fixed.
PostgreSQL 8.4.8


Steve Ebersole
September 4, 2012, 12:17 AM

The fix version is showing 3.6.10 in addition to 4.1, but I am not seeing changes for it on the 3.6 branch. However, we have been having problems with the Jira->GitHub connector, so it is possible the change did get ported but is just not showing up here because of that. Have you tried 4.1? 3.6 is no longer developed

Vaclav Topinka
September 4, 2012, 11:29 AM

Sorry for confusion - I double checked everything and this bug IS FIXED in 3.6.10. Problem I had is different (however seems to be caused by this fix) and is described in #HHH-7203.

Andre Campos
December 29, 2012, 9:26 PM

I'm using Hibernate 4.1.9 with Postgres 9.0 and this bug is still there. Here's the stack trace for the PSQL exception:

Caused by: org.postgresql.util.PSQLException: Valor inválido para tipo int : aereo [Sorry for the portuguese, but it says "invalid type for type int: aereo", where aereo is the discriminator column whose type is String]
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toInt(AbstractJdbc2ResultSet.java:2759)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getInt(AbstractJdbc2ResultSet.java:2003)
at org.hibernate.id.IdentifierGeneratorHelper.get(IdentifierGeneratorHelper.java:117)
at org.hibernate.id.IdentifierGeneratorHelper.getGeneratedIdentity(IdentifierGeneratorHelper.java:86)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:98)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 80 more

Andre Campos
December 29, 2012, 9:46 PM

For the ones lacking patience, you can remove the discriminator column and recreate it. That way the id becomes the first column on the table and the issue disappears. Just make sure to copy the data to another column and back in case you do it every time the the application starts and you have data on the table already.

Andre Campos
December 30, 2012, 4:26 PM

Disregard my comment about hibernate 4.1.8 and postgres 9.0; the bug has been fixed indeed. My class path was messed out during my tests here. My bad!


Steve Ebersole



Fix versions




Suitable for new contributors


Requires Release Note


Pull Request





Affects versions