简体   繁体   中英

Difference between Hibernate Automatic value generation strategies?

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

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM