Sometimes and seems randomly Hibernate executes query like that during persist operation:
select currval(\'MY_TABLE_NAME_
According to the hibernate documentation:
2.6.10. Using IDENTITY columns
For implementing identifier value generation based on IDENTITY columns, Hibernate makes use of its
org.hibernate.id.IdentityGenerator
id generator which expects the identifier to be generated by INSERT into the table. IdentityGenerator understands 3 different ways that the INSERT-generated value might be retrieved:
- If Hibernate believes the JDBC environment supports
java.sql.Statement#getGeneratedKeys
, then that approach will be used for extracting the IDENTITY generated keys.- Otherwise, if
Dialect#supportsInsertSelectIdentity
reports true, Hibernate will use the Dialect specific INSERT+SELECT statement syntax.- Otherwise, Hibernate will expect that the database supports some form of asking for the most recently inserted IDENTITY value via a separate SQL command as indicated by
Dialect#getIdentitySelectString
You can specify java.sql.Statement#getGeneratedKeys
explicitly in the following way:
true
You do not do it, so, hibernate somehow treat its value as false
. Then hibernate checks value of dialect.getIdentityColumnSupport().supportsInsertSelectIdentity()
. It is false
for your case. And then hibernate follows by the last way. See the implementation of the PostgreSQL81IdentityColumnSupport class. The method getIdentitySelectString generates exactly the same sql that you complained about.