'Where to set hibernate.id.new_generator_mappings property?

I've wasted too much time on this ...

I'm using oracle and I have a sequence (MY_TABLE_SEQ) defined which increments by 1.

In my Pojo I have:

@SequenceGenerator(name = "MY_SEQ", sequenceName="MY_TABLE_SEQ", allocationSize=50)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="MY_SEQ")

This gives me a unique constraint issue. From my understanding I need to set the following property:

hibernate.id.new_generator_mappings=true

I've tried setting in my hibernate.cfg.xml file but it does not seem to make any difference. I've come across server post to place in persistance.xml but this is a standalone app, no webcontainer.

Setting allocationSize=1 works but of course it hits the db on each insert to get the next sequence. Setting the above property is suppose to resolve it.



Solution 1:[1]

I haven't tried Oracle, but I had similar issues to yours inserting into an AS400 DB2 table.

I had to remove the identity flag on the id column on DB2 table - and instead used a custom jpa/hibernate sequence generator. This is set up on the pojo/entity annotation of the @ID entity field as you've done.

DB2 had been giving me errors about missing SYSIBM.SYSSEQUENCES table, so evidently hibernate (version 5.2), doesn't recognize the native DB2 identity designation. A custom sequence was and effective workaround.


On the @ID entity field:

@GeneratedValue(generator = "table", strategy=GenerationType.TABLE)

@TableGenerator(name = "table", allocationSize = 20)

This example allocates a pool of 20 sequence numbers each time it queries the table.


Next, create the required table Hibernate needs with columns that match the hibernate5 API - must be in lower case ... so put quotes around the names to work around the auto-upper casing that DB2 defaults to. The API will error out if these names are in caps.

Table: "hibernate_sequences"

example of 2 Columns used: "sequence_next_hi_value" (integer, not nullable, 0 default) "sequence_name" (character, sample length 20, not nullable, natural default)


In the configuration code for the dialect used - ex: Spring Boot programmatically, add these properties:

properties.put("hibernate.supportsSequences","false"); properties.put("hibernate.id.new_generator_mappings","false");

and in the *.properties file:

spring.jpa.properties.hibernate.dialect.supportsSequences=false spring.jpa.properties.hibernate.id.new_generator_mappings=false


Database systems are case-sensitive for schema/table/field names. Also watch for typos everywhere, incl. property names.


Be sure your pojo/entity only contains private fields that will be mapped to the table. Static finals such as serialVersionUID are ok.


I will be doing someething similar for SQL Server soon.

For MySQL, I had no issues using an identity column as defined in a table ID field to insert records, so didn't have to make all these changes. A simpler setup since hibernate recognizes the identity designation in MySQL. @GeneratedValue(strategy=GenerationType.IDENTITY) was all that was needed in the pojo.

I'm a newbie at all this, so always looking for better ways ... but this worked for now.

Solution 2:[2]

I set the property like this in the hibernate.cfg.xml file and it works !

<property name="hibernate.jpa.compliance.global_id_generators" value="true"/>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 npe