Java Reference
In-Depth Information
Next, we're going to introduce the @GeneratedValue annotation and see how it's used
to generate primary keys in these different ways.
Auto
The auto strategy is the JPA default and the simplest use of @GeneratedValue . Auto
frees the developer from any special database work and leaves the generation of the
primary key to whatever defaults are configured for the database. The next listing shows
what the User object would look like using the auto strategy.
Listing 9.11. Auto strategy
Identity
The identity strategy makes use of a special database column type that maintains its own
auto-incrementing number to uniquely identify rows in the table. Most databases sup-
port this column type: Microsoft SQL Server, IDENTITY ; MySQL, AUTO_INCREMENT ;
PostgreSQL, SERIAL ; Oracle does have support for this column type. When data is in-
serted into the table, the identity field will automatically increment and store that value as
the row's primary key. The following listing shows what the User object would look like
using the identity strategy.
Listing 9.12. Identity strategy
Search WWH ::




Custom Search