liveBook · Manning

Database sequence

As explained in Java Persistence with Spring Data and Hibernate epub

A database sequence is a database object that produces successive identifier values independently of row insertion.

A database sequence supplies numeric identifiers independently of row insertion. That matters when an ORM delays SQL INSERTs to batch work: persisting an entity can queue its insertion, while a pre-insert generator can assign its identifier before the row exists. With an auto-incremented column, column default, or trigger-generated key, the identifier is available only after INSERT. section 5.2.5

For a JPA entity, @GeneratedValue(generator = "ID_GENERATOR") names a generator configured separately from the entity. Hibernate’s enhanced-sequence strategy uses a native database sequence when the SQL dialect supports one; otherwise, it uses an extra table to emulate a sequence. A single database sequence can be shared across domain model classes. section 5.2.4 section 5.2.5

Listing 5.2 Hibernate identifier generator configured as package-level metadata
Path: Ch05/generator/src/main/java/com/manning/javapersistence/ch05
 /package-info.java
 
@org.hibernate.annotations.GenericGenerator(
  name = "ID_GENERATOR",
  strategy = "enhanced-sequence",                #1
  parameters = {
     @org.hibernate.annotations.Parameter(
        name = "sequence_name",                  #2
        value = "JPWHSD_SEQUENCE"
     ),
     @org.hibernate.annotations.Parameter(
        name = "initial_value",                  #3
        value = "1000"
     )
})
#1 The <kbd>enhanced-sequence</kbd> strategy produces sequential numeric values. If your SQL dialect supports sequences, Hibernate (or Spring Data JPA using Hibernate as a persistence provider) will use an actual database sequence. If your DBMS doesn’t support native sequences, Hibernate (or Spring Data JPA using Hibernate as a persistence provider) will manage and use an extra “sequence table,” simulating the behavior of a sequence. This gives you real portability: the generator can always be called before performing an SQL <kbd>INSERT</kbd>, unlike, for example, auto-increment identity columns, which produce a value on <kbd>INSERT</kbd> that has to be returned to the application afterward.
#2 You can configure the <kbd>sequence_name</kbd>. Hibernate (or Spring Data JPA using Hibernate as a persistence provider) will either use an existing sequence or create one when you generate the SQL schema automatically. If your DBMS doesn’t support sequences, this will be the special “sequence table” name.
#3 You can start with an <kbd>initial_value</kbd> that gives you room for test data. For example, when your integration test runs, Hibernate (or Spring Data JPA using Hibernate as a persistence provider) will make any new data insertions from test code with identifier values greater than 1,000. Any test data you want to import before the test can use numbers 1 to 999, and you can refer to the stable identifier values in your tests: “Load item with id 123 and run some tests on it.” This is applied when Hibernate (or Spring Data JPA using Hibernate as a persistence provider) generates the SQL schema and sequence; it’s a DDL option.

The package-level @GenericGenerator configuration names an enhanced-sequence generator and sets its sequence_name and initial_value parameters. section 5.2.4

Persisting an entity

Begin a JPA transaction, create and populate an Item, call em.persist(item), then commit. The Spring Data JPA variant calls itemRepository.save(item). In either case, do not equate the persistence call with an immediate SQL INSERT; whether an ID is available beforehand depends on the generator. section 5.2.4 section 5.2.5

Choose the generator explicitly when sequence names or other settings matter. JPA offers @SequenceGenerator and @TableGenerator for named generators; Hibernate’s @GenericGenerator exposes more Hibernate options and can be placed in package-info.java. GenerationType.TABLE instead keeps next values in an extra table that is read and updated before INSERTs. For identifiers needed before insertion, prefer enhanced-sequence over strategies that obtain keys only afterward. section 5.2.4 section 5.2.5

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains generator configuration, native-sequence fallback, and identifier timing; does not detail database sequence internals.
Examples
Shows package-level @GenericGenerator parameters and Item persistence through EntityManager.persist and Spring Data JPA save.
What it assumes
Assumes familiarity with JPA entities and SQL INSERTs while explaining generator configuration and insertion timing.
What you can do after
Operate/configure it with a named Hibernate generator and choose a pre-insert strategy when identifiers are needed before INSERT.
How it connects
Connects database sequences to JPA identifier generators, Hibernate enhanced-sequence, SQL INSERT batching, and auto-incremented columns.

Also explained in

Compare how 2 books explain database sequence →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.