Custom Sequence-Based ID Generators
Custom Sequence-Based ID Generators
www.thoughts-on-java.org
Implement a Custom, Sequence-Based IdGenerator
@Override
public Serializable generate(SharedSessionContractImplementor session,
Object object) throws HibernateException {
return valuePrefix
+ String.format(numberFormat, super.generate(session, object));
}
@Override
public void configure(Type type, Properties params,
ServiceRegistry serviceRegistry) throws MappingException {
super.configure(LongType.INSTANCE, params, serviceRegistry);
valuePrefix = ConfigurationHelper.getString(
VALUE_PREFIX_PARAMETER, params, VALUE_PREFIX_DEFAULT);
numberFormat = ConfigurationHelper.getString(
NUMBER_FORMAT_PARAMETER, params,
NUMBER_FORMAT_DEFAULT);
}
}
Please note that the configure method changes the value of the Type
type parameter, when it calls the configure method of the superclass.
This is necessary because the sequence value will be part of a String,
but Hibernate can’t handle String-based sequences. So, you need to
tell Hibernate to generate a sequence value of type Long and convert
it afterward.
www.thoughts-on-java.org
Implement a Custom, Sequence-Based IdGenerator
@Entity
public class Book {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "book_seq")
@GenericGenerator(
name = "book_seq",
strategy =
"org.thoughts.on.java.generators.StringPrefixedSequenceIdGenerator",
parameters = {
@Parameter(name =
StringPrefixedSequenceIdGenerator.INCREMENT_PARAM, value = "50"),
@Parameter(name =
StringPrefixedSequenceIdGenerator.VALUE_PREFIX_PARAMETER, value =
"B_"),
@Parameter(name =
StringPrefixedSequenceIdGenerator.NUMBER_FORMAT_PARAMETER, value =
"%05d") })
private String id;
...
}
www.thoughts-on-java.org
Implement a Custom, Sequence-Based IdGenerator
@Override
public Serializable generate(SharedSessionContractImplementor session,
www.thoughts-on-java.org
Object object) throws HibernateException {
Implement a Custom, Sequence-Based IdGenerator
@Override
public Serializable generate(SharedSessionContractImplementor session,
Object object) throws HibernateException {
return String.format(format, LocalDate.now(), 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(DATE_FORMAT_PARAMETER, params,
DATE_FORMAT_DEFAULT).replace("%", "%1$");
String numberFormat =
ConfigurationHelper.getString(NUMBER_FORMAT_PARAMETER, params,
NUMBER_FORMAT_DEFAULT).replace("%", "%2$");;
String dateNumberSeparator =
ConfigurationHelper.getString(DATE_NUMBER_SEPARATOR_PARAMETER,
params, DATE_NUMBER_SEPARATOR_DEFAULT);
this.format = dateFormat+dateNumberSeparator+numberFormat;
}
}
www.thoughts-on-java.org
Implement a Custom, Sequence-Based IdGenerator
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"book_seq")
@GenericGenerator(
name = "book_seq",
strategy =
"org.thoughts.on.java.generators.DatePrefixedSequenceIdGenerator",
parameters = {@Parameter(name =
DatePrefixedSequenceIdGenerator.INCREMENT_PARAM, value = "50")})
private String id;
...
}
www.thoughts-on-java.org
Implement a Custom, Sequence-Based IdGenerator
That Object contains the entity object for which the
PublisherCodePrefixedSequenceIdGenerator got called. You can use
it to access all attributes of that entity including all initialized
associations. In this example, I use it to get the value of the code
attribute of the associated Publisher entity.
@Override
public Serializable generate(SharedSessionContractImplementor session,
Object object) throws HibernateException {
return String.format(format, ((Book)object).getPublisher().getCode(),
super.generate(session, object));
}
@Override
public void configure(Type type, Properties params,
ServiceRegistry serviceRegistry) throws MappingException {
super.configure(LongType.INSTANCE, params, serviceRegistry);
String codeNumberSeparator = ConfigurationHelper.getString(
CODE_NUMBER_SEPARATOR_PARAMETER, params,
CODE_NUMBER_SEPARATOR_DEFAULT);
String numberFormat =
ConfigurationHelper.getString(NUMBER_FORMAT_PARAMETER, params,
NUMBER_FORMAT_DEFAULT).replace("%", "%2$");
this.format = "%1$s"+codeNumberSeparator+numberFormat;
www.thoughts-on-java.org
Implement a Custom, Sequence-Based IdGenerator
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"book_seq")
@GenericGenerator(
name = "book_seq",
strategy =
"org.thoughts.on.java.generators.PublisherCodePrefixedSequenceIdGenerator",
parameters = {
@Parameter(name =
PublisherCodePrefixedSequenceIdGenerator.INCREMENT_PARAM, value = "50
})
private String id;
...
}
www.thoughts-on-java.org