How to use existing Oracle sequence to generate id in hibernate?

后端 未结 10 1093
夕颜
夕颜 2020-12-04 19:53

I have legacy Oracle db with a sequence named PRODUCT_ID_SEQ.

Here is the mapping of Product class for which I need generate correct ids:<

10条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 20:18

    If you use javax.persistence.SequenceGenerator, hibernate use hilo and will possibly create large gaps in the sequence. There is a post addressing this problem: https://forum.hibernate.org/viewtopic.php?t=973682

    there are two ways to fix this problem

    1. In the SequenceGenerator annotation, add allocationSize = 1, initialValue= 1

    2. instead of using javax.persistence.SequenceGenerator, use org.hibernate.annotations, like this:

      @javax.persistence.SequenceGenerator(name = "Question_id_sequence", sequenceName = "S_QUESTION")

      @org.hibernate.annotations.GenericGenerator(name="Question_id_sequence", strategy = "sequence", parameters = { @Parameter(name="sequence", value="S_QUESTION") } )

    I have tested both ways, which works just fine.

提交回复
热议问题