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.
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
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.
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]
... 80 more
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.
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!