liveBook · Manning

@GeneratedValue

As explained in Java Persistence with Spring Data and Hibernate epub

The JPA @GeneratedValue annotation specifies that a persistence provider should generate an entity identifier value according to a configured generation strategy.

@GeneratedValue tells the JPA provider to generate an entity identifier when the entity is persisted. Place it beside @Id; without it, the application must assign the identifier before saving, as required for legacy databases or natural primary keys. section 5.2.4

Persisting an entity with a generated identifier

@Entity class Item { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Long id; public Long getId() { return id; } } Item item = new Item(); item.setName("Some Item"); em.persist(item); // The provider obtains the identifier during persistence. // item.getId() can then expose the generated value. section 5.2.4

Select a strategy through GenerationType:

  • AUTO lets Hibernate choose an appropriate strategy from the configured database SQL dialect; it is equivalent to leaving strategy unspecified. section 5.2.4
  • SEQUENCE obtains values from a database sequence, commonly HIBERNATE_SEQUENCE, separately before each INSERT. section 5.2.4
  • IDENTITY expects a database auto-increment primary-key column that generates the value during INSERT. section 5.2.4
  • TABLE reads and updates an auxiliary table holding the next numeric value, with a row for each entity class. section 5.2.4
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.

Customizes Hibernate identifier generation with a named enhanced sequence, sequence name, and initial value. section 5.2.4

AUTO is convenient, but explicitly choosing a strategy gives more control over identifier creation. Hibernate-specific generator configuration can also customize sequence naming and initial values. Identifier properties should not expose a public setter because primary keys do not change. section 5.2.4 section 5.2.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Compares four generation strategies and configuration implications, but defers provider internals beyond Hibernate-oriented behavior.
Examples
Uses an Item entity, Hibernate generator metadata, and Hibernate or Spring Data JPA persistence examples.
What it assumes
Assumes basic entity and primary-key mapping while explaining application-assigned identifiers and standard GenerationType choices.
What you can do after
Apply it via JPA by mapping @Id with an explicit or default generation strategy and persisting the entity.
How it connects
Connects entity identifiers with primary keys, application-assigned identifiers, database sequences, identity columns, and Hibernate generator configuration.

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.