Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate generates negative id values when using a sequence

I have a class with the following definition:

@Id
@SequenceGenerator(name = "SEQ_ACE_WORKERS_QUEUE_STATS_ID", sequenceName = "SEQ_ACE_WORKERS_QUEUE_STATS_ID", allocationSize = 500)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACE_WORKERS_QUEUE_STATS_ID")
@Column(name = "ID")
private long Id;

When we ran it on Jboss 4.2.3 it worked fine and generated the proper ID's (starting from 1000+)

Now we moved to jboss 7.1.1 and it generates negative ID's! (starting from -498 and going up)

Any idea why this might happen?

like image 764
Tomer Avatar asked Mar 25 '12 15:03

Tomer


People also ask

What is ID generator in hibernate?

A generator class is used to generate an ID for an object, which is going to be inserted in the database as a primary key. The ID is a unique identifier for the objects of the persistent class. We can use any generator classes in our application as per our requirements.

What is Generation type sequence in hibernate?

SEQUENCE is the generation type recommended by the Hibernate documentation. The generated values are unique per sequence. If we don't specify a sequence name, Hibernate will reuse the same hibernate_sequence for different types.

What is allocationSize in sequence generator?

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

What is the use of ID Generatedvalue strategy GenerationType identity?

IDENTITY This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence.


3 Answers

The new behaviour is the followings:

AllocationSize is a range of primary key values reserved for Hibernate. And the select seq.nextval from dual will be done only after hibernate consumed this range of primary keys.

So you must declare the same value on both allocationSize (Hibernate) and sequence increment by (DB)

When explicitly set allocationSize=500, e.g. on Oracle

create sequence SEQ_ACE_WORKERS_QUEUE_STATS_ID        MINVALUE 1         MAXVALUE 999999999999999999999999999         START WITH 1        INCREMENT BY 500         NOCACHE         NOCYCLE; 

Otherwise, you'll notice negative values or constraint errors raised from your DB because of primary key collisions.

When the app server is restarted, you'll notice the 'jump' between the latest primary key allocated, and the 'newly' sequence number selected at restart.

Final comment: default value is 50. So if you don't specify allocationSize on the Hibernate side, you must declare increment by 50 on the DB side.

like image 148
skay Avatar answered Sep 21 '22 11:09

skay


I just ran into this issue when migrating from JBoss 6.1 to JBoss 7.1.

According to the JBoss AS 7.1 JPA documentation ( https://docs.jboss.org/author/display/AS71/JPA+Reference+Guide#JPAReferenceGuide-Persistenceunitproperties),

JBoss 7.1 automatically sets several hibernate properties. One of the properties being set is hibernate.id.new_generator_mappings which activates new ID generators that use different algorithms and are not backwards compatible. Setting this property to false in your persistence.xml file will restore the old ID generator behavior.

The hibernate 4 documentation also has information regarding the new ID generators: http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html_single/#mapping-declaration-id-generator.

The hibernate documentation clearly states that the new ID generators are not enabled by default, but, as noted above, JBoss 7.1 is automatically enabling them.

like image 30
jrm Avatar answered Sep 21 '22 11:09

jrm


Setting hibernate.id.new_generator_mappings to false in my persistence.xml was just the first part of the solution to my problem:

To completely solve the problem I added the allocationSize to 1 in the @SequenceGenerator (which I was omitting).

like image 34
falsarella Avatar answered Sep 18 '22 11:09

falsarella