为@GenericGenerator设置allocationSize

时间:2014-12-18 00:40:36

标签: java hibernate jpa orm

我之前有@SequenceGenerator参数allocationSize=1

的代码

我需要使用自定义SequenceGenerator覆盖@GenericGenerator。但是,我找不到在自定义allocationSize策略类中指定GenericGenerator参数的方法。我该怎么办?

1 个答案:

答案 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);
    }

}