问题
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