自定义,基于序列的ID生成器Hibernate 6问题



我最近将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);
}

}

Since in Hibernate 6 ->org.hibernate.type.LongType已弃用,我得到错误

如何实现自定义,基于序列的ID生成器??

检查javadocs https://docs.jboss.org/hibernate/orm/6.1/javadocs/org/hibernate/type/BasicTypeRegistry.html后,我尝试了这个,它工作了:

super.configure(new TypeConfiguration().getBasicTypeRegistry().getRegisteredType(Long.class), params, serviceRegistry);

在Hibernate 6中,推荐实现这种类型的生成器的方法是实现BeforeExecutionGenerator

相关内容

  • 没有找到相关文章

最新更新