Java Reference
In-Depth Information
Sequence
The sequence strategy uses a database sequence to get auto-incrementing unique numbers.
This strategy is most popular with Oracle because Oracle doesn't have the identity column
type discussed in the previous section. To configure JPA to use a database sequence as the
primary-key generator, first create the sequence. The following SQL shows how to create
a sequence in Oracle:
CREATE SEQUENCE USER_SEQUENCE START WITH 1 INCREMENT BY 10;
Next, use @SequenceGenerator to configure a connection to the sequence; then con-
figure @GeneratedValue to use this sequence generator. The following listing shows
what the User object would look like using the sequence strategy.
Listing 9.13. Sequence strategy
In this listing there's first a @SequenceGenerator with name set to
USER_SEQUENCE _GENERATOR . This is the internal JPA name for this sequence,
and other JPA annotations will use this name when referring to the sequence. The se-
quenceName attribute is set to the actual name of the sequence in the database, which
in this case is USER_SEQUENCE . For the @GeneratedValue annotation, the
strategy is set to Generation_Type.SEQUENCE
and the generator is set to
the internal JPA name of the sequence
.
A @SequenceGenerator is sharable across the entire persistence unit. This means
a @SequenceGenerator doesn't need to be defined in the class it's used. Any
Search WWH ::




Custom Search