Who is responsible of the auto-incrementation of a primary key between MySQL and Hibernate?

时光怂恿深爱的人放手 提交于 2019-12-05 08:24:20

To use a MySQL AUTO_INCREMENT column, you are supposed to use an IDENTITY strategy:

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

Which is what you'd get when using AUTO with MySQL:

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

Which is actually equivalent to

@Id @GeneratedValue
private Long id;

If you specify GenerationType.IDENTITY , then the database will be in charge assigning the initial identifier. So, using

@GeneratedValue(strategy=GenerationType.IDENTITY)

would leave the DB in-charge.

It is definitely the database. Each db dialect has a slightly different way for Hibernate to then query what that newly assigned ID was so it can set it on your entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!