@GenericGenerator is a Hibernate annotation that registers a named identifier generator, either one of Hibernate's built-in strategies or your own class that implements IdentifierGenerator. You then point @GeneratedValue(generator = "...") at that name so Hibernate calls it to create the primary key before the insert. It is the classic way to get UUIDs, prefixed codes or other non-standard IDs, but since Hibernate 6.5 the annotation is deprecated in favour of typed alternatives such as @UuidGenerator and @IdGeneratorType.
This article shows how the annotation works, how to write a custom generator, and what to use instead on current Hibernate 6 and 7 releases.
@GenericGenerator does@GenericGenerator doesJPA only defines a few key strategies: AUTO, IDENTITY, SEQUENCE, TABLE and, since Jakarta Persistence 3.1, UUID. When none of them fits, Hibernate lets you plug in any generator through @GenericGenerator. The annotation has three attributes:
| Attribute | Purpose |
|---|---|
name | The name that @GeneratedValue(generator = "...") refers to. |
strategy | A built-in alias such as uuid2, or the fully qualified name of an IdentifierGenerator class. |
parameters | An array of @Parameter(name, value) pairs passed to the generator when it is configured. |
The annotation lives in org.hibernate.annotations, so it ties the entity to Hibernate. If you may switch JPA providers, prefer the standard strategies.
A custom generator implements org.hibernate.id.IdentifierGenerator. In Hibernate 6 and later the generate method returns Object (in Hibernate 5 it returned Serializable). The example below builds IDs such as EMP-3F2A9C1B, using a prefix that is passed in as a parameter.
The configure method receives the values declared in @GenericGenerator(parameters = ...). The generate method runs once per new entity and the returned value becomes the primary key.
Declare the generator on the identifier field and reference it by name. The key column must be a string type that is long enough for the generated value.
When you call entityManager.persist(new Employee("Asha")), Hibernate invokes the generator, assigns the value to id and then runs the insert. Because the ID is created in the application before the insert, it is available immediately after persist, which also lets Hibernate batch inserts. This is different from IDENTITY, where the database assigns the value during the insert and batching is disabled.
Hibernate ships several generators you can use as the strategy value without writing a class. The common ones are:
| Alias | Result |
|---|---|
uuid2 | A java.util.UUID or string UUID, RFC 4122 based. |
sequence | A database sequence, with options for name and allocation size. |
increment | Max value plus one, safe only with a single writer. |
assigned | You set the ID yourself before persist. |
Many of these now have proper annotations. Prefer those, because a misspelt string in strategy only fails at startup.
In Hibernate 6.5 @GenericGenerator was deprecated. The recommended replacements depend on what you need:
@UuidGenerator (Hibernate 6.2 and later), or the standard @GeneratedValue(strategy = GenerationType.UUID) from Jakarta Persistence 3.1.@SequenceGenerator and @TableGenerator annotations or the JPA ones.@IdGeneratorType.A UUID key without any custom class is now a single annotation:
For custom logic, define an annotation and attach your generator to it. The generator class needs a constructor that accepts the annotation, the annotated member and a creation context.
You then write @Id @OrderNumber private String id;. The annotation is type-checked, discoverable in the IDE and carries its own parameters, which removes the string-typed strategy and parameters from @GenericGenerator. Check the signatures against the Hibernate version you use, because the generator SPI changed between 6.0 and 6.5.
Custom generators are not always the right answer.
@UuidGenerator(style = UuidGenerator.Style.TIME) produces time-based values.@GenericGenerator deprecated?Yes. It has been deprecated since Hibernate 6.5, although it still works. New code should use @UuidGenerator, the sequence and table generator annotations, or a custom @IdGeneratorType annotation.
@GenericGenerator with Spring Data JPA?Yes. Spring Data JPA uses your JPA provider, so a Hibernate-specific annotation on an entity works as usual. Spring itself does not change how the ID is generated.
@GeneratedValue and @GenericGenerator?@GeneratedValue says that the key is generated and which generator name or strategy to use. @GenericGenerator defines a named generator that @GeneratedValue can refer to.
persist?Generators run when the entity is persisted, not when you construct it. After persist, the ID field is populated.
@GenericGenerator binds a name to a Hibernate ID generator, built in or custom, which @GeneratedValue then uses. It is deprecated in modern Hibernate. Use @UuidGenerator for UUIDs, the sequence annotations for numeric keys, and @IdGeneratorType for custom logic.