Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of deprecated [org.hibernate.id.SequenceGenerator] and Unique constratint violations when jpa.properties.hibernate.id.new_generator_mappings:true

Tags:

hibernate

jpa

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

like image 952
Can't Tell Avatar asked Apr 03 '17 07:04

Can't Tell


People also ask

What is Hibernate ID New_generator_mappings?

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 .

What is allocationSize in @SequenceGenerator?

allocationSize. (Optional) The amount to increment by when allocating sequence numbers from the sequence.

What is allocationSize in Hibernate?

allocationSize - (Optional) The amount to increment by when allocating sequence numbers from the sequence.

How would you implement a custom sequence based id generator?

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.


1 Answers

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.

like image 93
Can't Tell Avatar answered Oct 27 '22 02:10

Can't Tell