2

Quote from documentation:

detached

the entity has an associated identifier, but is no longer associated with a persistence context (usually because the persistence context was closed or the instance was evicted from the context)

Does it mean that hibernate keep references to all objects that have been ever persistence? But then it would cause memory lick.

Or it means that entity has an id that corresponds to an entity in a database then it would lead to two conclusions: 1) It can be checked only querying a database. 2) A detached entity can become transient if some one third would delete the entity from the database.

Hideo Kuze
  • 31
  • 2
  • Are you asking about the hibernate specific Session.evict() behavior? Or hibernate implementation of JPA detach()? Or JPA in general? – Nicholas Hirras Jan 24 '18 at 15:37
  • If for example persistence context was closed. And detached entities from it were used in another persistence context. How would it distinguish between detached and transient entities? – Hideo Kuze Jan 24 '18 at 15:49

4 Answers4

0

When an entity is first created in application using the new() operator, it remains in transient state.It can move to Persistent state when you associate it with a session by calling Session.save() method. When you close() the session or evict() that object from session, it moves to detached state. You can again move a detached object to Persistent state by calling Session.update() or Session.saveOrUpdate() method.

Satya Mahesh
  • 339
  • 1
  • 3
  • 14
  • What marks that object is detached or transient? Which methods in hibernate would behave differently on objects of different types? I do get when state transition happen but I didn't get what it affects and how this state is stored. Or it is just classification term that doesn't affect anything? Then why it is needed as it brings additional complexity with it in the documentation? – Hideo Kuze Jan 24 '18 at 15:54
  • Transient and Detached object is not associated with hibernate session, there is a key difference between them. First, detached object was associated with Hibernate session in past and it has representation in database, on the other hand, Transient object is never associated with hibernate and it doesn't have any representation in database. – Satya Mahesh Jan 24 '18 at 16:53
0

I didn't find anything in the documentation regarding this, but I believe it does track detached entities ... for example, According to JPA specs, you have the case when an entity was fetched (but its lazy relations are not) ... If you detach this entity then merge it again , It considers the the relations unloaded, but if you set the relation to null, then after merging it will be also set to null ... so how will it differentiate if it doesn't keep record of the detached ... there are also some scenarios in the cascade.MERGE behavior that might tell that it keeps record of the previously detached entities .... again this is what I expect, no documentation there

osama yaccoub
  • 1,884
  • 2
  • 17
  • 47
0

saveOrUpdate() does the following:

if the object is already persistent in this session, do nothing

if another object associated with the session has the same identifier, throw an exception

if the object has no identifier property, save() it

if the object's identifier has the value assigned to a newly instantiated object, save() it

if the object is versioned by a or , and the version property value is the same value assigned to a newly instantiated object, save() it

otherwise update() the object

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html#objectstate-saveorupdate

The strategy is almost save when using Spring Data JPA.

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.entity-persistence.saving-entites.strategies

Mani Rai
  • 665
  • 2
  • 9
  • 22
0

If an entity has an identifier (Let's say a primary key) Hibernate considers that entity is in the detached state. At this stage Hibernate is in a doubt. To confirm 100% that an entity is in the detached state, there should be a record available in the database corresponding to the entity's identifier.

Transient state means a new record. When an entity is considered to be in the transient state, then in the database there should not be a record available corresponding to that entity's identifier.