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

lyr7nygr  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(199)

我最近将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生成器??

3pmvbmvn

3pmvbmvn1#

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

相关问题