hibernate IdGeneration
hibernate IdGeneration
https://www.baeldung.com/hibernate-identifiers
Simple Identifier
using @Id to a single property of one of these types: Java primitive and primitive wrapper types, String, Date, BigDecimal, BigInteger.
Generated Identifier
Auto
the persistence provider will determine values based on the type of the primary key attribute. This type can be numerical or UUID.
For numeric values, the generation is based on a sequence or table generator,
For UUID values will use the UUIDGenerator.
Identity
expects values generated by an identity column in the database, meaning they are auto-incremented.
|
SEQUENCE
This generator uses sequences if they're supported by our database, and switches to table generation if they aren't.
|
SEQUENCE is the generation type recommended by the Hibernate documentation.
Composite Identifiers
Finally, if we're going to access parts of the composite key individually, we can make use of @IdClass, but in places where we frequently use the complete identifier as an object, *@EmbeddedId* is preferred.
The primary key class must fulfill several conditions:
- it should be defined using @EmbeddedId or @IdClass annotations
- it should be public, serializable and have a public no-arg constructor
- it should implement equals() and hashCode() methods
embeded
|
IdClass
The @IdClass annotation is similar to the @EmbeddedId, except the attributes are defined in the main entity class using @Id for each one.
|
others
TableGenerator uses an underlying database table that holds segments of identifier generation values.
we can define our custom generator by implementing the *IdentifierGenerator* interface.
eg
ublic class MyGenerator
implements IdentifierGenerator, Configurable {
private String prefix;
public Serializable generate(
SharedSessionContractImplementor session, Object obj)
throws HibernateException {
String query = String.format("select %s from %s",
session.getEntityPersister(obj.getClass().getName(), obj)
.getIdentifierPropertyName(),
obj.getClass().getSimpleName());
Stream ids = session.createQuery(query).stream();
Long max = ids.map(o -> o.replace(prefix + "-", ""))
.mapToLong(Long::parseLong)
.max()
.orElse(0L);
return prefix + "-" + (max + 1);
}
public void configure(Type type, Properties properties,
ServiceRegistry serviceRegistry) throws MappingException {
prefix = properties.getProperty("prefix");
}
}
ublic class MyGenerator |
https://stackoverflow.com/questions/10041938/how-to-choose-the-id-generation-strategy-when-using-jpa-and-hibernate