Explain the advantages and disadvantages of detached objects.
Advantages :- Detached objects passing can be done across layers upto the presentation layer without using Data Transfer Objects.
- At the time of using long transactions by the user which needs long think-time, it is suggested to split these transactions into some transactions. The detached objects get modified apart from the transaction scope which then can be re-attached to a new transaction.
Disadvantages :- The usage of detached objects are cumbersome and cryptic. It is suggested not to be cluttered with the session, if possible.
- It is recommended to use DataTransferObjects and DomainObjects that is used to maintain separation between the user interfaces and the Service.
Explain the advantages and disadvantages of detached objects.
- Optimistic concurrency control
- Each interaction with the persistent store occurs in a new Session. However, the same persistent instances are reused for each interaction with the database.
- The application manipulates the state of detached instances originally loaded in another Session and then reattaches them using Session.update(), Session.saveOrUpdate(), or Session.merge().
foo.setProperty("bar");
session = factory.openSession();
Transaction t = session.beginTransaction();
session.saveOrUpdate(foo); // Use merge() if "foo" might have been loaded already
t.commit();
session.close();
- Again, Hibernate will check instance versions during flush, throwing an exception if conflicting updates occurred.
- You can also call lock() instead of update(), and use LockMode.READ (performing a version check and bypassing all caches) if you are sure that the object has not been modified.