Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting allocationSize for @GenericGenerator

I had previous code for a @SequenceGenerator with parameter allocationSize=1

I needed to override the SequenceGenerator with a custom @GenericGenerator. However I don't see a way to specify my allocationSize parameter inside my custom GenericGenerator strategy class. What can I do?

like image 717
James Avatar asked Nov 01 '22 12:11

James


1 Answers

I have got the answer for this, yes it is possible my creating a CustomGenerator and then using @GenericGenerator and in that CustomGenerator use the code like below:

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

}
like image 119
jayash samaiya Avatar answered Nov 09 '22 16:11

jayash samaiya