@GeneratedValue with strategy=GenerationType.AUTO generates repeated value after restart

后端 未结 1 1836
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 21:39

I have a hibernate entity with an ID configured as

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

The creation of new eleme

相关标签:
1条回答
  • 2021-02-05 22:10

    If you use AUTO, Hibernate will choose one of the strategies to generate your id. From the Reference:

    AUTO - either identity column, sequence or table depending on the underlying DB.

    So you have to see the ids being generated to see which strategy Derby is using. Although it looks like, it resets the generator everytime you restart your app. Try setting

    <prop key="hibernate.hbm2ddl.auto">update</prop>
    

    You could quickly fix it using a sequence generator though. Like:

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="my_seq_gen")
    @SequenceGenerator(name="my_seq_gen", sequenceName="ENTITY_SEQ")
    private Long id;
    

    Where ENTITY_SEQ is the name of the sequence in your database (you create one manually).

    0 讨论(0)
提交回复
热议问题