Créer un générateur hibernate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class PublisherCodePrefixedSequenceIdGenerator extends SequenceStyleGenerator {
public static final String CODE_NUMBER_SEPARATOR_PARAMETER = "codeNumberSeparator";
public static final String CODE_NUMBER_SEPARATOR_DEFAULT = "_";
public static final String NUMBER_FORMAT_PARAMETER = "numberFormat";
public static final String NUMBER_FORMAT_DEFAULT = "%05d";
private String format;
@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;
}
}
Mysterious Mallard