liveBook · Manning

GenerationType enum

As explained in Java Persistence with Spring Data and Hibernate epub

The GenerationType enum defines standardized JPA strategies for generating entity identifier values.

GenerationType standardizes JPA strategies for generating entity identifier values. Combine it with @Id and @GeneratedValue when the database or persistence provider should assign a primary key; omit @GeneratedValue when the application assigns identifiers manually. section 5.2.4

Choose a strategy according to database capabilities and control requirements:

  • AUTO lets Hibernate select a suitable strategy from the configured database SQL dialect and is equivalent to @GeneratedValue() without settings. section 5.2.4
  • SEQUENCE calls a database sequence before each INSERT; the default expected name is HIBERNATE_SEQUENCE. section 5.2.4
  • IDENTITY expects a database auto-increment column that generates the numeric key during INSERT. section 5.2.4
  • TABLE reads and updates an auxiliary table holding the next numeric value, with one row per entity class. section 5.2.4

Persisting an entity with a generated identifier

Annotate an entity identifier with @GeneratedValue(strategy = GenerationType.SEQUENCE), then persist a new Item. Hibernate obtains the generated value and assigns it as part of persistence. section 5.2.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Compares four strategies and their database behavior, while deferring detailed provider-specific customization.
Examples
Uses persisted Item instances, Hibernate sequence configuration, and generated-key database results.
What it assumes
Assumes entity and primary-key mapping basics while distinguishing generated identifiers from application-assigned identifiers.
What you can do after
Configure identifier generation with @GeneratedValue and select a strategy suited to the database schema.
How it connects
Connects entity identifiers with @Id, @GeneratedValue, database sequences, auto-increment columns, and application-assigned identifiers.

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.