fastcodejava
fastcodejava

Reputation: 41117

Detached object in hibernate

What is the benefit of detached object? What value do they provide? Thanks.

Upvotes: 0

Views: 1095

Answers (3)

Sandeep
Sandeep

Reputation: 31

Suppose you need to execute a long transaction(user input taking time ) as well as object to be used as a presentation layer element. Now your object needs to play a double role.

  1. As a detached object (To be used as a presentation layer object )
  2. Reattached object (To complete transaction)

How is that possible?

Break the transaction in two transactions. as soon as the inner transaction will complete Object will be detached and it can work in the presentation layer. As soon as presentation work finish It will enter into another transaction and will work as an attached object.

So this all is possible due to attached object.

Upvotes: 3

communityUser
communityUser

Reputation: 21

When there are long transactions that need to be handled, then it is logical to break the long transaction up into two or more transactions. Detached objects can be used to carry the information up to UI layer and can be re-attached to a new transaction with help of another session.

On the other hand, it is not very advisable to use detached objects in such scenarios rather DTO's (Data Transfer Objects) can serve the purpose fairly well here.

Upvotes: 1

rsenna
rsenna

Reputation: 11982

When you need to keep a object "alive" between distinct hibernate sessions. Example: in web applications, if you're using the session-per-request pattern, and you need to keep the same hibernate entity between many pages.

Please take a look at the Hibernate docs:

Chapter 10: Working with objects

Chapter 11: Transactions and Concurrency

Upvotes: 5

Related Questions