0

Actually these two are very good questions:

How Hibernate differs transient and detached entities?

Check for detached entity in Hibernate

I have actually read this many time, and check some posts in stackoverflow. A lot of answers are just copied from the persistence state definition without further explanation.

Let see the example below:

Person person = new Person();
person.id = 123; // 123 identifier exist in database.
person.name = "abc";

Hibernate doc state that for a transient instance, it must be:

  • instantiated using the new operator
  • has no persistent representation in the database
  • no identifier value has been assigned

This doesn't look like a transient instance, it doesn't fulfill the definition.

It doesn't look like a detached instance too, obviously it is not detached from a closed session.

So, is this a transient or detached instance?

Sam YC
  • 10,725
  • 19
  • 102
  • 158
  • As I understand ... (no identifier value has been assigned) by or registered to the persistence context – osama yaccoub Feb 22 '18 at 11:36
  • also check my answer to one of the questions you mentioned : https://stackoverflow.com/questions/48426067/how-hibernate-differs-transient-and-detached-entities/48926627#48926627 – osama yaccoub Feb 22 '18 at 12:01
  • Sorry I don't quite understand your answer, do you mean, hibernate track the instance whether it is transient or detached by their identifier only? – Sam YC Feb 23 '18 at 02:16
  • It seems to be keeping record of previously detached entities somehow – osama yaccoub Feb 23 '18 at 07:28

1 Answers1

0

from Pro JPA 2.1 :

An entity has a persistent identity when there exists a representation of it in the data store; that is, a row in a database table. If it is not in the database, then even though the in-memory entity may have its identity set in a field, it does not have a persistent identity

So persistence id is only considered after the entity is already persisted, not by assigning a value to the id element

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