liveBook · Manning

GenerationType.AUTO

As explained in Java Persistence with Spring Data and Hibernate epub

A JPA identifier-generation strategy in which the persistence provider selects an appropriate strategy for the configured database.

GenerationType.AUTO delegates identifier-strategy selection to Hibernate or Spring Data JPA, which consults the configured database SQL dialect. It is equivalent to @GeneratedValue() without an explicit strategy. section 5.2.4

Generated identifier during persistence

Map an entity identifier with @Id and @GeneratedValue(strategy = GenerationType.AUTO), then persist normally:\n\nItem item = new Item();\nitem.setName("Some Item");\nitem.setAuctionEnd(Helper.tomorrow());\nem.persist(item);\n\nThe provider queues the INSERT and may assign the identifier before execution; with an INSERT-generated key, item.getId() can remain null until the INSERT occurs. section 5.2.5

Evaluate AUTO against the identifier strategy your application actually requires:

  • Expect the dialect to select a sequence or identity strategy; the result can be convenient but may be less portable or efficient than an explicit choice. section 5.2.5
  • Choose GenerationType.SEQUENCE for an explicit database sequence, IDENTITY for an auto-incremented column, or TABLE for a generator table. section 5.2.4
  • Prefer enhanced-sequence when consistent, portable values before INSERTs and configurable optimizers matter; it uses a native sequence or emulates one with a table. section 5.2.5

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains dialect selection, identifier timing, and alternatives, but stops short of detailed provider-specific AUTO algorithms.
Examples
Uses @Id mappings and Hibernate or Spring Data JPA persistence of an Item to illustrate generated primary keys.
What it assumes
Assumes familiarity with entity identifiers, @Id, @GeneratedValue, persistence providers, and configured database dialects.
What you can do after
Configure generated identifiers with AUTO and evaluate whether an explicit strategy better serves portability, efficiency, or pre-insert access.
How it connects
Connects GenerationType.AUTO with SQL dialects, database sequences, identity columns, table generators, and enhanced-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.