After upgrading to Hibernate 5 I am getting the error Found use of deprecated [org.hibernate.id.SequenceGenerator]
. I found this answer which has a code snippet mentioning how to solve the problem.
I would like to know how that solution works. Does that code snippet do the same thing as the @SequenceGenerator
annotation? If so why was the SequenceGenerator
deprecated?
My annotations are from javax.persistence
package. I would prefer to not add hibernate specific stuff in my code. In the answer I have linked there is strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
which is hibernate dependent(at least on the runtime). Is there a way to achieve this?
When I set hibernate.id.new_generator_mappings:true
I get unique constraint violation errors
id. new_generator_mappings that directs how identity or sequence columns are generated when using @GeneratedValue . In JBoss EAP 6, the default value for this property is set as follows: When you deploy a native Hibernate application, the default value is false .
allocationSize. (Optional) The amount to increment by when allocating sequence numbers from the sequence.
allocationSize - (Optional) The amount to increment by when allocating sequence numbers from the sequence.
If you want to use a custom generator, you need to define the generator in a @GenericGenerator annotation and provide the fully-qualified classname as the strategy. You can also configure a set of parameters that will be provided to the configure method when Hibernate instantiates the generator.
To answer my own question,
The above deprecation warning comes because Spring Boot 1.5 sets
jpa.properties.hibernate.id.new_generator_mappings: false
. If it was set to true, Hibernate will internally use the SequenceStyleGenerator
and the warning will not come.
But there would be a difference in the logic that is executed to get the next value of the sequence.
With the above setting set to false, org.hibernate.id.SequenceGenerator
would have been used, and this does not seem to use the allocationSize
parameter of @SequenceGenerator
. It will always call the database to get the next value of the sequence. There is another SequenceHiLoGenerator
and quoting from its javadoc, An IdentifierGenerator that combines a hi/lo algorithm with an underlying oracle-style sequence that generates hi values.
So this can be used together with INCREMENT BY
when creating the Orcale Sequence and that generator would not call the database for each insert.
With the above setting set to true, Hibernate will by default use a PooledOptimizer
which does use allocationSize
parameter. Due to this difference in how the ids are generated, I got the unique constraint violation error.
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