HomeНаука и техникаRelated VideosMore From: Thoughts On Java

How to generate primary keys with JPA and Hibernate

79 ratings | 4082 views
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! ➜ http://bit.ly/2cUsid8 Join the free Member Library: https://goo.gl/dtyIIC Read the accompanying post: https://www.thoughts-on-java.org/jpa-generate-primary-keys/ Want to connect with me? Blog: http://www.thoughts-on-java.org/ Twitter: https://twitter.com/thjanssen123 Facebook: https://www.facebook.com/thoughtsonjava
Html code for embedding videos on your blog
Text Comments (15)
Abhishek Danej (3 months ago)
Very clear and precise! thanks!
Thoughts On Java (2 months ago)
Ajay Dangi (3 months ago)
I want to use pk as string .is it possible in JPA?
Ajay Dangi (2 months ago)
thanks to reply but it doesn't work. not create table in mysql.
Thoughts On Java (2 months ago)
Yes, you just need to annotate an attribute of type String with the @Id annotation
Lakshman (7 months ago)
Thanks. Very simple and easy to understand with your explanation.
Thoughts On Java (7 months ago)
Thanks :)
Mohamed Jammal (8 months ago)
Thank you very much!
puspender Tanwar (1 year ago)
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?
puspender Tanwar (1 year ago)
I am using SEQUENCE, but still facing EntityExistsException exception. @Id @SequenceGenerator(name = "SequenceLocIdGenerator", sequenceName = "LINE_LOCQTY_JPA_ID_SQ") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceLocIdGenerator") @Column(name="LINE_LOCQTY_JPA_ID") private Long lineLocqtyJPAId; And when I use this sequence, I get the below logs: Hibernate: select saurabh.LINE_LOCQTY_JPA_ID_SQ.nextval from dual Hibernate: select saurabh.LINE_LOCQTY_JPA_ID_SQ.nextval from dual 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.
puspender Tanwar (1 year ago)
Hi Thorben, I have raised the complete details for question on https://stackoverflow.com/questions/48020829/javax-persistence-entityexistsexception-with-sequencegenerator. It would be great help if you could have a look. Thanks
Thoughts On Java (1 year ago)
The annotations seem to be ok. Do you set any ideas manually? Do you try to persist the same entity multiple times?
Santiago Nivelo (1 year ago)
Great video, I have a doubt. If I need to keep the order of the id, I mean no spaces between each récord, what's the most properly option?
Ahmad Hosny (1 year ago)
We use technical, numerical IDs.
Thoughts On Java (1 year ago)
I do the same in most of my projects. As I said in the video, that's almost always the fastest and easiest option

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.