Issue
I'm really confused between detached and transient entity. According to the definition, a transient entity is not associated with a session and does not have a DB record. A detached entity is associated with a session and has a DB record. So for ex. if I have a Person table. There is a record in the person table with id as 2. So if I do the following:
Person p = new Person();
p.setId(2);
Will the entity p be considered detached since it has a DB record but is not associated with a session? Or will it be considered transient? If it is transient then is the following code a correct example of creating a detached entity?
Person p = session.load(Person.class,2);
session.evict(p); // does this make the entity detached?
I'm really confused as to which of the above approaches create a detached entity so any insights are highly appreciated! Thanks!
Solution
Lets be correct:
When a managed entity object is serialized and then deserialized, the deserialized entity object (but not the original serialized object) is constructed as a detached entity object since is not associated with any EntityManager.
And transient entity field:
Transient entity fields are fields that do not participate in persistence and their values are never stored in the database (similar to transient fields in Java that do not participate in serialization).
Now you can conclude the answer of your questions.
Answered By - Sai prateek