我最近将Hibernate从5迁移到6
但是面临LongType的问题,这在Hibernate 6中被弃用
这是我在Hibernate 5中的实现
public class StringPrefixedSequenceIdGenerator extends SequenceStyleGenerator {
public static final String VALUE_PREFIX_PARAMETER = "valuePrefix";
public static final String VALUE_PREFIX_DEFAULT = "";
private String valuePrefix;
public static final String NUMBER_FORMAT_PARAMETER = "numberFormat";
public static final String NUMBER_FORMAT_DEFAULT = "%d";
private String numberFormat;
@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);
}
}
由于在Hibernate 6 -〉org.hibernate.type.LongType已弃用,因此出现错误
如何实现自定义的基于序列的ID生成器??
1条答案
按热度按时间3pmvbmvn1#
在Hibernate 6中,实现这种生成器的推荐方法是实现
BeforeExecutionGenerator
。