A named identifier generator is a reusable configuration that separates identifier-generation settings from individual entity mappings. Entities reference it with @GeneratedValue(generator = "ID_GENERATOR"), while the generator defines the sequence or table strategy and its settings. section 5.2.4
Choose the configuration mechanism according to portability and required control:
- Use JPA’s @SequenceGenerator or @TableGenerator for named database sequences or generator tables. section 5.2.4
- Use Hibernate’s @GenericGenerator when Hibernate strategies or detailed parameters are required; package-info.java can hold this metadata. section 5.2.4
- Share one database sequence across entity classes when generated values only need uniqueness within each table. 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.
The package-level configuration names an enhanced-sequence generator, selects JPWHSD_SEQUENCE, and sets its initial value to 1000. section 5.2.4
Persisting an entity
After configuring the generator, persist an Item through JPA with em.persist(item) or through Spring Data JPA with itemRepository.save(item); the primary key is generated during persistence.
