10

After migrating to Hibernate 5.2.7, I seem to be getting incorrect values for the id field.

My code:

@Id @SearchableId
@GeneratedValue(strategy=GenerationType.AUTO, generator="hms_seq_gen")
@SequenceGenerator(name="hms_seq_gen", sequenceName="patregn_seq")
protected Integer ID;

Hibernate fires this query:

select nextval ('patregn_seq')
which gives 5367. The last value in the id field in the table is 5358.

And I get this
ERROR: duplicate key value violates unique constraint "patientregistration_pkey" [java] Detail: Key (id)=(5318) already exists.

I am sure this question is similar to this and this, but I am forced to ask because the solution given there does not work for me:

I added

<property value="true" name="hibernate.id.new_generator_mappings"/>

to my persistence.xml, but to no avail. Any help would be greatly appreciated.

Lemmy
  • 2,437
  • 1
  • 22
  • 30
Thomas Abraham
  • 105
  • 1
  • 1
  • 7

1 Answers1

20

Actually when you migrate to the new Hibernate version 5.2.7, hibernate.id.new_generator_mappings defaults to true.

For backward compatibility you should change this flag to false.

For more information please search the userguide for new_generator_mappings: - http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html

Clijsters
  • 4,031
  • 1
  • 27
  • 37
idmitriev
  • 4,619
  • 4
  • 28
  • 44
  • Thanks for your suggestion. It works. But I'm a bit confused now. As per Steve Ebersole's comment at http://stackoverflow.com/questions/12737092/is-there-a-way-to-dynamically-choose-a-generatedvalue-strategy-using-jpa-annota, it should be set to true to get the behaviour described - which is what I thought I needed. Also, when I set the property to false, I get a warning: `Found use of deprecated [org.hibernate.id.SequenceGenerator] sequence-based id generator; use org.hibernate.id.enhanced.SequenceStyleGenerator instead.` This, even when I am using JPA SequenceGenerator – Thomas Abraham Feb 15 '17 at 04:32
  • Yeah Thomas, it generates a warning. From User Guide - "Existing applications may want to disable this (set it false) for upgrade compatibility from 3.x and 4.x to 5.x." Actually it should be set to true when you start developing your application, but for legacy - you should disable it, otherwise your application will fail. – idmitriev Feb 15 '17 at 10:23