• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

javax.persistence.EntityExistsException with JPA SequenceGenerator

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am inserting some records in Oracle DB. For the uniqueness, I am using SequenceGenerator. Below is the code:

XxspPoInLineLocqty is having @ManyToOne relation with XxspPoInLine. When I am persisting XxspPoInLine entity, I am receiving below error:
I looked on the internet and found some solutions for this:
1. Use allocationSize=1
Since I am having 5000+ XxspPoInLineLocqty , this would be the worst option I could apply. I tried this as well but after 40min my network got fluctuate and persisting got failed. I cannot use this option as it degrade the performance.

2. Increse the value of allocation size I increased the allocationSize=500, but still faced the same issue at different identifier(#372).
Even tried GenerationType.AUTO, but no luck.
Below is the sequence I am using:
I am still confused why this issue is coming up. I didn't understand the root cause of this. Can someone explain me the root cause of this exception, and what should be the work around?

Note : Before inserting the records, I deleted all the rows from table. So the table is blank when I execute the above sequnce code.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestions please.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved it by changing my Sequence IncrementBy value to the value as I set in allocationSize of JPA.


The explanation why I was getting issue for previous Sequence(IncrementBy value 1):
1. When the JPA required the Id value, it hits the DB Sequence and asks for the unique value, Sequence returns 7641.
2. Now JPA has a starting point for ID, and based on allocationSize=20, JPA creates next 20 values itself and generate 7641 to 7660 unique IDs.
3. Now when all of these IDs get consumed, JPA asks DB Sequence for next unique value. And since the last value returned was 7641, sequence returns 7642(because INCREMENTBY value was 1 ).
4. JPA gets the 7642 and creates next 20 values in cache.
5. The time when JPA tries to allocate these values to the ID, it finds 7642 already has been assigned to an entity object(at step 2).

How INCREMENTBY 20 solved the issue:
When Ids get consumed at step 3, JPA asks for next value to Sequence. Last value return was 7641, so this time it will increment it by 20 and return 7661. JPA creates values from 7661 to 7680 and uses them. And hence no unique key violation issue.
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic