Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Hibernate entity and ID Mapping

 
Ranch Hand
Posts: 235
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 235
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23252
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 235
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23252
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic