Difference between Hibernate Automatic value generation strategies?

只愿长相守 提交于 2021-02-08 08:11:18

问题


What is the difference between these two Automatic value generation strategies?

 1. @GeneratedValue
 2. @GeneratedValue(strategy=IDENTITY)

回答1:


This is like following:

AUTO Indicates that the persistence provider should pick an appropriate strategy for the particular database.

IDENTITY Indicates that the persistence provider must assign primary keys for the entity using database identity column.

SEQUENCE Indicates that the persistence provider must assign primary keys for the entity using database sequence column.

TABLE Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.

Refer to the API here http://docs.oracle.com/javaee/5/api/javax/persistence/GenerationType.html




回答2:


If you don't set the strategy attribute, it defaults to AUTO. From the Hibernate docs:
AUTO: selects IDENTITY, SEQUENCE or TABLE depending upon the capabilities of the underlying database.




回答3:


The difference is that @GeneratedValue uses AUTO strategy as default while @GeneratedValue(strategy=IDENTITY) uses IDENTITY strategy

Here are the different options for strategy

AUTO - Indicates that the persistence provider should pick an appropriate strategy for the particular database.

IDENTITY - Indicates that the persistence provider must assign primary keys for the entity using database identity column.

SEQUENCE - Indicates that the persistence provider must assign primary keys for the entity using database sequence column.

TABLE - Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.



来源:https://stackoverflow.com/questions/8473910/difference-between-hibernate-automatic-value-generation-strategies

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