2020-06-20

Hibernate JPA @GenericGenerator Example

@GenericGenerator

The @GenericGenerator annotation can be used to configure any Hibernate identifier generator.

GenericGenerator Generator annotation describing any kind of Hibernate generator in a generic (de-typed) manner.

@GenericGenerator

@GenericGenerator allows integration of any Hibernate org.hibernate.id.IdentifierGenerator implementation, including any of the specific ones discussed here and any custom ones.

To make use of the pooled or pooled-lo optimizers, the entity mapping must use the @GenericGenerator annotation:

Example : Pooled-lo optimizer mapping using @GenericGenerator mapping

@Entity(name = "Product")
public static class Product {

@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "product_generator"
)
@GenericGenerator(
name = "product_generator",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
@Parameter(name = "sequence_name", value = "product_sequence"),
@Parameter(name = "initial_value", value = "1"),
@Parameter(name = "increment_size", value = "3"),
@Parameter(name = "optimizer", value = "pooled-lo")
}
)
private Long id;

@Column(name = "p_name")
private String name;

@Column(name = "p_number")
private String number;

//Getters and setters are omitted for brevity

}

No comments:

Post a Comment