JPA - PK attribute issue after INSERT

peter.petrov :

JPA (hibernate provider) and Postgres 11

I have this configuration for my Id property (it's a single column/attribute PK).

For some reason after doing an INSERT into this table, JPA calls

select currval('yb.asset_store_id_seq')

I guess (I am not sure) it's doing it to refresh the Id of the newly inserted entity or maybe for some similar reason.

But there's no such sequence. My sequence is named yb.asset_store_seq and my table is yb.asset_store. My entity class is AssetStore.

So... how do I get rid of this issue? Basically JPA is somehow making up this sequence name out of nowhere. Or am I missing something ?!

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false, insertable=false, updatable=false)
private Integer id;

On the DB level, the sequence is owned by the table.

Here is the stack trace.

    Hibernate: 
        select
            currval('yb.asset_store_id_seq')
    Mar 25, 2020 3:45:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    WARN: SQL Error: 0, SQLState: 42P01
    Mar 25, 2020 3:45:02 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
    ERROR: ERROR: relation "yb.asset_store_id_seq" does not exist
      Position: 16
    Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet


    ...................


        Caused by: org.postgresql.util.PSQLException: ERROR: relation "yb.asset_store_id_seq" does not exist
  Position: 16
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
SternK :

First of all the hibernate documentation does not suggest to use GenerationType.IDENTITY strategy at all:

It is important to realize that using IDENTITY columns imposes a runtime behavior where the entity row must be physically inserted prior to the identifier value being known.

This can mess up extended persistence contexts (long conversations). Because of the runtime imposition/inconsistency, Hibernate suggests other forms of identifier value generation be used (e.g. SEQUENCE).

There is yet another important runtime impact of choosing IDENTITY generation: Hibernate will not be able to batch INSERT statements for the entities using the IDENTITY generation.

But if you can not change it, and you use the JDBC3+ driver and JRE1.4+. I would suggest to explicitly define:

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

Because according to this

hibernate.jdbc.use_get_generated_keys (e.g. true or false)

Allows Hibernate to use JDBC3 PreparedStatement.getGeneratedKeys() to retrieve natively-generated keys after insert. You need the JDBC3+ driver and JRE1.4+. Disable this property if your driver has problems with the Hibernate identifier generators. By default, it tries to detect the driver capabilities from connection metadata.

It will allow to avoid your problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=361191&siteId=1