The object can be Transient,Persistent, Removed or detached.
if you are Persistent or Removed you are in the Persistence Context.
Transient: when you create a normal POJO from an Entity you are in transient mode, so for example
Bank x = new Bank(); this is a Transient object, it is not associated with a database record,
Persistent: it means the entity (the object) is associated with a database record, the object here is in the Persistence Context. if you do any manipulation on this entity, this manipulation will be reflected in the DB as well.
Removed: you are in the persistence context and scheduled for deletion.
Detached: if you are an object in the persitent state however we close the Persistence Context ==> you will be in the Detached state.
What is a persistence context
Persistence Context is an object that provides us with a place to put all the entities which are in the persistent state. All the changes that we do on these entities will be in the Persistence Context and will be sync with the DB later.
We get a Persistence Context by using a session
when you openSession() you are creating a new persistence context.
if you get() or load() from the database you will be in the Persistent state.
you can save() or saveOrUpdate() to move from Transient to Persistent
you can call delete() on a persistent entity to make it removed.
if the persistence context is closed the entity will be detached, if you still have a reference to that entity, you can attach it again and make it persistent by calling update() or saveOrUpdate().
also you can detach an entity (without closing the Persistence Context) by calling the evict() method.
here is an example
as you can see here after the bank was detached we called
session2.delete(bank), YOU CANNOT DO THAT IN JPA THIS IS SOMETHING ONLY IN HIBERNATE
as you can see, firstly we openSession() (i.e create a Persitence Context).
then we create account, trans1 and trans2 (those are Transient Entitiy now).
then we do a simple check session.contains(account) to check if these entities are in the PersistenceContxt, of course will return FALSE.
then we begin the transaction,
session.save(account) // now account is in Persistence state.
then we commit() which flushes the data to the database.
then we close() the context
Retrieving Entities from DB
we use get(CLASS TYPE, ID), as you can see get() executes a select statement
as you can see 2 get() and one select statment, the first get runs a select statmnet, the net get() for the same ID will get the result from the cache of the session.
another method to retrieve is load()
as you can see the update statement runs when we do commit().
as you can see we get() --> select statment
then we check session.contains()--> true, the object is in the session.
then we delete()--> as you can see the delete() will not generate a delete statment to the database, the record will be delete when we flush(i.e. when we commit).
then we check session.contains() again --> false because the object is detached.
then transaction.comit()--> delete statment, we can see 2 delete statment because there is a cascade.ALL.
VERY IMPORTANT: after the bank object is deleted it should not be used anymore and it should be garbage collected because it is now deleted from the DB.
Reattaching detached object.
we do that with update() or saveOrUpdate() methods
the flush will happen if you call flush() or transaction.commit() or sometimes when we do select, a flush may occur