• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

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: 21311
140
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: 21311
140
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.
 
please buy this thing and then I get a fat cut of the action:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!