Fellow developers, I'm finding it hard to get the sequence generation configured.
I inherited a persistent class with the following id field definition:
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
All of my classes inherit from this class.
The main thing I want: I need to override the strategy of id generation to use HiLo. It has to be in the cfg.xml since it's the only place that I have control over.
This thing looked promising:
<property name="hibernate.id.new_generator_mappings">true</property>
However I couldn't choose the optimize, or the increment size (I want to go HiLo)
<property name="hibernate.id.increment_size">50</property>
<property name="hibernate.id.optimizer">hilo</property>
didn't work, nor
<property name="increment_size">50</property>
<property name="optimizer">hilo</property>
nor
<property name="optimizer">org.hibernate.id.enhanced.HiLoOptimizer</property>
The NoopOptimizer is always chosen.
Any tip will help. Thanks a lot in advance.
So far there is no way to do this. but also I couldn't find a way to do this with the new
This one will use the new SequenceStyleGenerator but I couldn't configure this. The parameters used for configuration such as "optimizer" and "increment_size" are not passed to the method that configures the generator.
My point is, I do not know how this "hibernate.id.new_generator_mappings" is supposed to be used to make use of a hilo optimizer and configure properly and my reverse engineering capability proved unreliable.
To use hilo I had to do this @GeneratedValue(strategy = SEQUENCE) which works for testing but it's not a good option for me.
thanks @xwoker but i couldn't get it done.
Let's do some investigation (looking at the Hibernate sources):
The
AnnotationBinder
defines how AUTO is mapped:If you don't use the NewGeneratorMappings you're stuck with the native implementation. Hibernate will resolve that depending on the database (see this answer to see what that will be for your specific database).
But since you used the NewGeneratorMappings we have to look at the
SequenceStyleGenerator
:Assuming the right Properties are passed and you have an "optimizer" property in them then your value should be returned. This value is used to call
OptimizerFactory.buildOptimizer
(as the first parameter):So either it finds your value "hilo" (or your "HiLoOptimizer"), instantiates it and returns it or it will log some error message. If you find no error message in your logs I would check whether the properties in the cfg.xml are actually really used. Try to access them with
getProperties()
on yourorg.hibernate.internal.SessionFactoryImpl
instance.