Today, I will show you how to generate unique primary key values with JPA and Hibernate.
But before we start, I want to know how you get the primary key values in your application? Do you use natural keys or UUIDs or do you generate technical IDs?
Please post a comment below and tell me about it.
I prefer to generate simple, numerical, technical IDs like you can see in the following code snippet instead of using natural keys which often require the combination of multiple attributes.
Technical IDs are easier to manage and all involved systems, mainly the database and Hibernate, can index them very efficiently. This allows you to focus on the business logic of your application and avoids performance issues.
If you like this video, please give me your thumbs up and share it with your friends and co-workers.
Like my channel? Subscribe!
Join the free Member Library:
Read the accompanying post: https://www.thoughts-on-java.org/jpa-generate-primary-keys/
Want to connect with me?
We need to take extra care with Sequence of Oracle DB.
In my recent project, I was working with Oracle database. I chose the GenerationType.SEQUENCE, but ran into issues. I was having OneToMany relationships so whenever there were 20+ child records, I was getting EntiytExistException. The issue I found was that the Oracle Sequence was not in sync with Hiberanate caching(allocationSize). The allocationSize I kept was 20 and the Sequence INCREMENTBY = 1.
So suppose if the sequence returned 10, Hibernate generates 10 to 29 in the cache(based on allocationSize). So when those Ids got consumed, Hibernate ask Oracle Sequence to return next value, and this time it returns 11(INCREMENTBY=1). Hibernate tries to use 11 as Id and stuck as EntityExistException, because 11 was already generated and used by Hibernate in its cache.
So for the resolution to this, I had to kept the INCREMENTBY = allocationSize. This resolved the issue.
Is it the expected behaviour with Oracle, or I missed something?
I am using SEQUENCE, but still facing EntityExistsException exception.
@SequenceGenerator(name = "SequenceLocIdGenerator", sequenceName = "LINE_LOCQTY_JPA_ID_SQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceLocIdGenerator")
private Long lineLocqtyJPAId;
And when I use this sequence, I get the below logs:
javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.bcone.oracle.ebs.model.XxspPoInLineLocqty#68]
Any clue what could be the issue, as even I am using SEQUENCE for generating ID, hibernate is complaining for ID.
Hi Thorben, I have raised the complete details for question on
It would be great help if you could have a look.
Looking for someone like-minded? Chances are you’ve already got one thing in common.
15,000 New members.
Soulmates sees over 15,000 new faces every month.
#1 Customer service.
Feel safe and secure, with help from our award-winning customer service team.
Recently joined Soulmates.
Meet some of our latest members.
Online Dating Success Stories.
Intrepid explorers who’ve found the person of their dreams.
Kristian & Zoe.
"We met through Soulmates four years ago. Although were both based in London, the chances of us meeting by other means were slim. We just got married! Zoe is the most wonderful woman Ive ever met, and I know we are going to have an extremely happy life together."
Laura & Phil.
"Phil and I got married just over a year after we met on my first ever internet date through Guardian Soulmates. Weve been recommending the site to friends ever since. Thank you!" (Photography credit to Michael OSullivan)
Robin & Alec.
"I just wanted to let you know that you have another success story! My boyfriend, who I met via your site about two years ago, just proposed and we are getting married next year. THANK YOU!"
Alex & Rosie.
"Rosie and I met through Soulmates and were married in October, just 17 months later. Were wonderfully happy together!"
How does it work?
Find your Soulmate in three easy steps.
Register your free account today.