A
A
Alexey Titov2019-09-28 18:10:01
Spring
Alexey Titov, 2019-09-28 18:10:01

How to correctly configure hibernate to use GenerationType.IDENTITY when adding a record to the DB?

I'm trying to figure out a bunch of spring jpa + hibernate. Initial data:
- DB - postgresql 10
- To generate the entity id, I use sequence in the database
- not SpringBoot - deploy war in Tomcat
Entity:

@Entity
@Table(name = "category")
public class Category {

    @Id
    @Column(name = "category_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
//остальные члены класса
}

Repository for working with the database:
@Repository
public interface CategoryRepository extends JpaRepository<Category, Integer> {
}

Problem - when I try to add a record to the database, I get an error:
org.postgresql.util.PSQLException: ERROR: relation "category_category_id_seq" does not exist
. hibernate, instead of relying on the sequence in the database when adding a record (it should do this in accordance with the annotation @GeneratedValue(strategy = GenerationType.IDENTITY)) tries to calculate the id from the category_category_id_seq table, which does not exist.
At the same time, getting records from the database or updating existing ones works correctly. I checked the operation of the sequence by manually adding a record to the database.
upd:
I rummaged through the hibernate sources and found in the dialect for my database that a query like select currval ('" + table + '_' + column + "_seq') is used to calculate the identifier, i.e. there is a reversal to sequence, and it at me is called not so.
category_category_id_seq

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question