Why does Hibernate execute select currval in persist occasionally?

后端 未结 1 1870
余生分开走
余生分开走 2021-01-23 07:00

Sometimes and seems randomly Hibernate executes query like that during persist operation:

select currval(\'MY_TABLE_NAME_         


        
相关标签:
1条回答
  • 2021-01-23 07:22

    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:

    1. If Hibernate believes the JDBC environment supports java.sql.Statement#getGeneratedKeys, then that approach will be used for extracting the IDENTITY generated keys.
    2. Otherwise, if Dialect#supportsInsertSelectIdentity reports true, Hibernate will use the Dialect specific INSERT+SELECT statement syntax.
    3. 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:

    <property name="hibernate.jdbc.use_get_generated_keys">true</property>
    

    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.

    0 讨论(0)
提交回复
热议问题