我之前有@SequenceGenerator
参数allocationSize=1
我需要使用自定义SequenceGenerator
覆盖@GenericGenerator
。但是,我找不到在自定义allocationSize
策略类中指定GenericGenerator
参数的方法。我该怎么办?
答案 0 :(得分:2)
我已经得到了答案,是的,我可以创建一个CustomGenerator,然后使用@GenericGenerator,并在CustomGenerator中使用如下代码:
public class UseExistingOrGenerateIdGenerator extends SequenceHiLoGenerator {
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);
return id != null ? id : super.generate(session, object);
}
@Override
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
params.put(org.hibernate.id.SequenceGenerator.SEQUENCE, "HIBERNATE_SEQUENCE");
params.put(SequenceHiLoGenerator.MAX_LO, String.valueOf("49"));
super.configure(type, params, dialect);
}
}