When does an Object Become Detached

myCar” is a persistent object at this stage.
Session session1 = sessionFactory.openSession();
Car myCar = session1.get(Car.class, carId);
session1.close();
once the session is closed “myCar” becomes a detached objectyou can now pass the “myCar” object all the way upto the presentation tier. It can be modified without any effect to your database table.
myCar.setColor(“Red”); //no effect on the database
When you are ready to persist this change to the database, it can be reattached to another session as shown below:
Session session2 = sessionFactory.openSession();
Transaction tx = session2.beginTransaction();
session2.update(myCar); //detached object ”myCar” gets re-attached
tx.commit(); //change is synchronized with the database.
session2.close()