Manually update the counter used for generation of primary key in Hibernate?

前端 未结 2 452
悲&欢浪女
悲&欢浪女 2021-01-24 16:41

In my spring project, the tables in database are created automatically by Hibernate using my entity classes as base, but I insert some default values in the table manually (usin

2条回答
  •  没有蜡笔的小新
    2021-01-24 17:40

    The problem here might be that you declare the id as a primitive instead of a wrapper.

    So instead of:

    private int id;
    

    You should have:

    private Integer id;
    

    When you create the entity with the id is initialized as 0, instead of NULL.

    That's why you get duplicate id constraint violation exceptions.

    Only when the id is NULL the AUTO generation strategy will delegate the id assignment to the database.

提交回复
热议问题