• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 712
  • Last Modified:

merge in hibernate

Here is an example of merge()   . I don't understand it.

Session session = sessionFactory1.openSession();
Transaction tx = session.beginTransaction();
Item item = (Item) session.get(Item.class, new Long(1234));
tx.commit();
session.close(); // end of first session, item is detached
 
item.getId(); // The database identity is "1234"
item.setDescription("my new description");
Session session2 = sessionFactory.openSession();
Transaction tx2 = session2.beginTransaction();
Item item2 = (Item) session2.get(Item.class, new Long(1234));
Item item3 = session2.merge(item); // Success! ----------------------->LINE 1
tx2.commit();
session2.close();

See LINE 1 in the above code .

Q1)  I see item2 is never used in the code . whats the use of item2 then ?  I'm confused here.Do I really need this piece of code ?  

Q2) when we do the session2.merge(item) . It merges to whom ?    is not it should be  session2.item2.merge(item) ?
0
cofactor
Asked:
cofactor
3 Solutions
 
ManishLeadCommented:
1.I think there is no need to item2.
2. After changing description you are merging it, so database table should be updated with description.
0
 
objectsCommented:
1) Its there just to show what merge() does. After calling merge item3 will reference the same object as item2 does ie. item2 == item3

2) It 'merges' any persistent objects with the same key
0
 
cofactorAuthor Commented:
not yet clear.  still have the confusion.

Here is what I understand in steps ....I'm writing below ....Could you please tell whether my understanding is correct or not ?


Item item2 = (Item) session2.get(Item.class, new Long(1234));------>get from DB and put in session

Item item3 = session2.merge(item); ---->merge 'item' with 'item2' because they have same key '1234'.

so, item2 and item will be merged because they have the same key '1234' ...and item3 will be formed.

definitely item2 and item3 are different , because item2 does not have description field set  but item3 has got description field after merge.


Also,Item item2 = (Item) session2.get(Item.class, new Long(1234));  is a important step , otherwise hibernate will not find the object with the same key as item to merge.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ManishLeadCommented:
Hibernate will first check whether a Persistent instance of that type already exists in the persistent
context. It uses the object identifiers to check on this existence. If another instance exists, it copies the state of the Detached object (item above) into the existing Persistence object (item2 above). If no other instance exists, Hibernate just reattaches the Detached object.
0
 
cofactorAuthor Commented:
..so that means if I remove this line  
Item item2 = (Item) session2.get(Item.class, new Long(1234));

There will be no exception ...hibernate will just reattach 'item'  .
0
 
Kevin CrossChief Technology OfficerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now