@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
