wood burning stoves*
The moose likes Object Relational Mapping and the fly likes Reattaching of objects in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Reattaching of objects in Java" Watch "Reattaching of objects in Java" New topic
Author

Reattaching of objects in Java

Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
Hi,
I am learning using advanced features of Hibernate and I have come across the concept of "reattaching an object to a new session".
I went thru Hibernate in Action and read whole of the chapter on this topic(ch 9 working with objects). I could understand that you can reattach an object by calling session.lock, session.merge or session.save, session.update (there might be some more ways)

I wanted to try out an example to better understand the concept so that I can know the practical application. I am struggling to visualize a practical scenario where I would require to re-attachment of objects. I googled and most of the posts on reattaching were related to GUI scenarios.

So my questions are:
1. Is reattachment applicable for GUI scenarios
2. Is there a scenario and associated code example which I can see to learn the concept.

I have spent enough time and not able to code a practical scenario. Can somebody help me with my questions above. Please let me know if there are any other documentation which I can go thru myself.

Thanks for reading,
Satish
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

One of the scenarios is with a thick client application. You send objects across to thick client. The session was closed after the objects were sent. These objects have been modified by user & these modifications now need to be persisted. In this case you would reattach detached objects a session.
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hi Satish,

You are more or less right by saying that reattachment generally comes in case of GUI scenario. It could be a huge performance hit and/or application might go short of resources if detachment approach is not followed.

The example mentioned by Rishi is very much relevant.

Lets say you are using yahoo and want to edit your profile and assuming Yahoo makes use of hibernate to load your profile. In that case, when you make request for edit, yahoo server will open a session for you, execute the query to get your profile and pass it to the UI layer. (In this case I am not closing the session).

When the edit page comes on the screen, you take your own sweet time to change the data and then submit. Now on the server side, you can simply use the session which was open earlier for you with out doing reattachment.

What's the drawback of not using detachment concept in this case, application would very soon go out of DB connections if many users use the application and do DB related stuffs.

How do we get out of this scenario --> Uee detached concept.

User clicks edit --> server opens session --> profile gets loaded --> session gets closed (which eventually releases connection object) --> UI is loaded with the data --> you make changes and submit --> server opens a new session, reattach the profile object with a new session and updates in DB.

Regards,
Naresh Waswani


Naresh Waswani
Pankaj Kumarkk
Ranch Hand

Joined: Apr 17, 2011
Posts: 108
Hi Naresh,
Thanks for the detailed answer. It has helped me understand the concept.

I have few related questions:
So as you explained that reattachment is useful when you want to make the same object persistent again in Hibernate. So does this mean that the GUI has to ensure that it uses the same object handle as sent by the dao layer and resends the object pointing to same object handle.

e.g
Dao fetches contact object(lets say it's memory location is 123456)
Dao send to Gui (say JSP) the same object ie the object with memory location 123456
The GUI uses the object to display and let's say it makes some changes to some attributes in contact object(with object handle pointing to 123456 memory location)
The GUI passes this object(with object handle pointing to 123456 memory location) to DAO
The DAO opens a new session and reattaches it using say session.update() method and persist the data

Question:
1. If the GUI had sent a new contact object with same data(but different memory handle) then would the reattachment process not work
2. Why would I want to reattach. In above scenario I could very have done the same thing without using the reattachment concept(i.e just open a session and save/update the object again). I understand the concept. I am failing to understand the use of this. Does Hibernate guarantee some behavior out of box if we reattach

Is there any reference/documentation which explains this more.

Thanks
Satish


Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

So as you explained that reattachment is useful when you want to make the same object persistent again in Hibernate. So does this mean that the GUI has to ensure that it uses the same object handle as sent by the dao layer and resends the object pointing to same object handle.

No, it can be different reference also. In case of thick clients the data will be sent over the network & the reference that is being reattached will be different. Also once once obejcts are detached from session Hibernate does not keep track of them

1. If the GUI had sent a new contact object with same data(but different memory handle) then would the reattachment process not work

It will work

Why would I want to reattach. In above scenario I could very have done the same thing without using the reattachment concept(i.e just open a session and save/update the object again). I understand the concept. I am failing to understand the use of this. Does Hibernate guarantee some behavior out of box if we reattach

Makes life easy, I don't have to track updates, inserts on the object. I just attach it back & Hibernate persists the changes. In the absence of this, I will have to keep track of these changes. Basically i will have to load object & then merge the chages done to the loaded object, with the reattach feature Hibernate does this for me. You can reattach using save OR saveOrUpdate or merge, there are specefic guidelines on which method should be used in which scenario.

 
GeeCON Prague 2014
 
subject: Reattaching of objects in Java