Hibernate throws PK violation error

I am using hibernate with jboss 4.2.3, and everything works, now I moved the code to Jboss 7.1.1, and suddenly I started to get:

Caused by: org.hibernate.exception.ConstraintViolationException: ORA-00001: unique constraint (OBLICORE.PK_ACE_WORKERS_QUEUE_STATS_ID) violated 

Also the generated identifier is negative.

An entity that fails is defined as such:

 @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; 

I checked the sequence in Oracle and seems to be OK (As I said, it worked before with jboss 4.2 and nothing has changed on the DB side since the migration).

I tried to write Hibernate request logs, but could not find this request, and I also registered a specific call that continues this class, and saw that it was called only once.

+4
source share
3 answers

Check out this question: the sequence of a sleeping oracle creates a big gap

It should be a Hibernate sequence generator that, by default, runs the Hi / Lo algorithm, and the return values โ€‹โ€‹overflow. You can try using the hibernation-dependent annotation by default for older GenericGenerator(name="blah", strategy="sequence") behavior GenericGenerator(name="blah", strategy="sequence") or set allocationSize=1 .

If you rely on increasing your sequence by a value greater than 1, you will have to use a different generator. Or maybe this is enough to set hibernate.id.new_generator_mappings to false , but this is within the scope of the new question.

+7
source

When we changed Hibernate to use a new generator, I used the following script to fix the sequences:

  DECLARE v NUMBER; BEGIN FOR r IN (select sequence_name from user_sequences) LOOP EXECUTE IMMEDIATE 'ALTER SEQUENCE '|| r.sequence_name ||' INCREMENT BY 50'; EXECUTE IMMEDIATE 'SELECT '|| r.sequence_name ||' .NEXTVAL FROM DUAL' INTO v; END LOOP; END; / 

If your allocSize is 500, you must change "INCREMENT BY 50" to "INCREMENT BY 500".

+1
source

If the generated id value is not so critical in your project, try using @GeneratedValue(strategy = GenerationType.AUTO) this strategy will generate an identifier automatically, increasing the last one. Hope this will be helpful for you.

0
source

Source: https://habr.com/ru/post/1403633/


All Articles