This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hibernate entity and ID Mapping

 
Ranch Hand
Posts: 230
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Have a nice day. would like to have a clarification regarding ID generator and states of an entity in hibernate. Request to share your comments on this. We've some states with hibernate entity(Transient, Persistent, Detached), In which state an entity assigned its Pirmary key(Assigned)?. I think,Persistent state if we have specified GeneratorStrategy as either SEQUENCE OR IDENTITY. If not then we've to set this ID manually before calling session.save(). What's your infer on this? Thanks so much.
 
Mohammed Sardar.
Ranch Hand
Posts: 230
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the below from one of the link "To persist the changes to a transient object, you would have to ask the session to save the transient object to the database, at which point Hibernate assigns the object an identifier and marks the object as being in persistent state." So, I realize an entity get assigned with an identifier during "Transient" state itself and then move to persistent state....
 
Saloon Keeper
Posts: 22485
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your terminology is a little fuzzy there. But in JPA (not just Hibernate), a Generator is a mechanism for automatically assigning a primary key value when persisting a new object. Once persisted, that key's property value is accessible just like any other Entity property. Generators come in several varieties depending on where they look to determine what the next assignable key value is.

Some DBMS's have their own internal sequence generators also. So when defining a JPA Entity key that way, you should use Generator type SEQUENCE.
 
Mohammed Sardar.
Ranch Hand
Posts: 230
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Your terminology is a little fuzzy there. But in JPA (not just Hibernate), a Generator is a mechanism for automatically assigning a primary key value when persisting a new object. Once persisted, that key's property value is accessible just like any other Entity property. Generators come in several varieties depending on where they look to determine what the next assignable key value is.

Some DBMS's have their own internal sequence generators also. So when defining a JPA Entity key that way, you should use Generator type SEQUENCE.



Thanks Tim but my doubt was at which state JPA/Hibernate assigns the ID to the Entity ? Let me know if my understanding is wrong !!!
 
Tim Holloway
Saloon Keeper
Posts: 22485
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a state, it's an action. When you are using a generator the ID is generated and assigned when you do the initial save() for the Entity.
 
Crusading Chameleon likes the size of this ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic