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

后端 未结 10 1092
夕颜
夕颜 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:17

    Here is a working example with annotations, this way, the existing DB sequence will be used (you can also use the "sequence" strategy, but with less performance on insertion) :

    @Entity
    @Table(name = "USER")
    public class User {
    
        // (...)
    
        @GenericGenerator(name = "generator", strategy = "sequence-identity", parameters = @Parameter(name = "sequence", value = "USER_SEQ"))
        @Id
        @GeneratedValue(generator = "generator")
        @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
        public Long getId() {
            return this.id;
        }
    

提交回复
热议问题