Calling next value of a sequence in jpa

前端 未结 2 1452
情书的邮戳
情书的邮戳 2021-01-12 10:11

I have a class mapped as an Entity to persist it in a database. I have an id field as the primary key so every time the object is persisted the value of the id is retrieved

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 10:52

    See another question/answers on the subject of using sequence defined elsewhere than id fields. You can create a fake entity with one field (of type Long id). Connect it to the sequence you defined in the DB. Then create a CrudRepository implementation for that entity and call its save() method with an empty instance of the fake entity object you defined. Hibernate will run for you a "select YOUR_SEQ.NEXTVAL from dual" query.

提交回复
热议问题