Error when using sequence for primary key in postgres and Hibernate

Viewed 359

I'm using JPA/Hibernate and postgres as my DB

I created a sequence in postgres like this:

CREATE SEQUENCE player_sequence
INCREMENT 20
START 1;

I want Hibernate to use the above sequence for primary key.

The id part of the entity is:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "player_seq")
@SequenceGenerator(name = "player_seq", sequenceName = "player_sequence", allocationSize = 20)
private Integer id;

When I create a new player and try to save it via EntityManger.persist method I get the following error:

Hibernate: select next_val as id_val from player_sequence for update org.hibernate.id.enhanced.TableStructure$1$1 execute ERROR: could not read a hi value org.postgresql.util.PSQLException: ERROR: column "next_val" does not exist

I don't understand what I do wrong

EDIT:

Here is content of persistence.xml:

enter image description here

1 Answers

Try to correct your persistence.xml in this way:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">

    <persistence-unit name="my-persistence-unit">
        <description>...</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>


        <properties>
            <!--  It was wrong !!!  -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL10Dialect" />

            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/basketball" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="1234" />
            
            <property name="hibernate.show_sql" value="true" />

        </properties>

    </persistence-unit>

</persistence>
Related