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?
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With