Generate Custom Identifier in Hibernate
Sometimes, we want to generate our table's primary key with more custom information. Custom Identifiers contains more information for us which helps us to distinguish and identify the records. It adds more meaning to an identifier.
For example, an event Identifier like - 20220219000045 has two parts, the first part is the date and the second part is the sequence generated number.
YYYYMMDD + 6 digit number generated by the sequence
Let's first create an entity called Event and add a Sequence Id generator. We need to specify the Identifier generator as a strategy that will be used by Hibernate to generate Identifier for the column. You can provide parameters for sequence like optimization strategy (OPT_PARAM), database sequence name (SEQUENCE_PARAM), etc.
@Entity
@Table( name = "EVENT" )
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "eventIdGenerator")
@GenericGenerator(
name = "eventIdGenerator",
strategy = "com.demo.EventIdGenerator",
parameters = {
@Parameter( name = SequenceStyleGenerator.OPT_PARAM, value = "hilo"),
@Parameter( name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "EVENT_ID_SEQ"),
@Parameter( name = SequenceStyleGenerator.INITIAL_PARAM, value = "1"),
@Parameter( name = SequenceStyleGenerator.INCREMENT_PARAM, value = "20")
})
@Column(name = "EVENT_ID")
private Long eventId;
@Column
private String eventType;
@Column
private LocalDate eventDate;
//Getters & Setters
...
}
package com.demo;
public class EventIdGenerator extends SequenceStyleGenerator {
public static final String DATEFORMAT_PARAM = "dateFormat";
public static final String DATEFORMAT_DEFAULT = "%tY%tm%td";
public static final String NUM_FORMAT_PARAM = "numberFormat";
public static final String NUM_FORMAT_DEFAULT = "%010d";
private String format;
/**
* Generates 18 digit long number, first 8 digits are taken
* from current date as YYYYMMDD and remaining from sequence with
* padding value of 0.
*
*/
@Override
public Serializable generate(SessionImplementor session,
Object object) throws HibernateException {
Date currentDate = new Date();
return Long.parseLong(String.format(format, currentDate, currentDate, currentDate, super.generate(session, object)));
}
@Override
public void configure(Type type, Properties params,
ServiceRegistry serviceRegistry) throws MappingException {
super.configure(LongType.INSTANCE, params, serviceRegistry);
String dateFormat = ConfigurationHelper.getString(DATEFORMAT_PARAM, params, DATEFORMAT_DEFAULT).replace("%", "%");
String numberFormat = ConfigurationHelper.getString(NUM_FORMAT_PARAM, params, NUM_FORMAT_DEFAULT).replace("%", "%");
this.format = dateFormat+numberFormat;
}
}
Comments
Post a Comment