.What’s the Difference Between load() and get()

load() get() Only use the load() method if you are sure that the object exists. If you are not sure that the object exists, then use one of the get() methods load() method will throw an exception if the unique id is not found in the database. get() method will return null if the  unique id is not found in the database. load() just returns a … Read more

Write your own Strategy with Interceptor.isUnsaved()

When you reattach detached objects, you need to make sure that the dependent objects are reattached as well. It appears that you are asking about using an interceptor in the context of advanced Java programming, and specifically, you want to know about a strategy involving the Interceptor.isUnsaved() method. However, the provided information is not sufficient … Read more

How does Hibernate Distinguish Between Transient (i.e. newly instantiated) and Detached Objects

Hibernate uses the “version” property, if there is one. If not uses the identifier value. No identifier value means a new object. This does work only for Hibernate managed surrogate keys. Does not work for natural keys and assigned (i.e. not managed by Hibernate) surrogate keys. Hibernate uses the concept of entity state to distinguish … Read more

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 … Read more

What are the Benefits of Detached Objects

Pros: When long transactions are required due to user think-time, it is the best practice to break the long transaction up into two or more transactions. You can use detached objects from the first transaction to carry data all the way up to the presentation layer. These detached objects get modified outside a transaction and … Read more