Solved

merge in hibernate

Posted on 2010-09-01
7
706 Views
Last Modified: 2013-11-24
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
Comment
Question by:cofactor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Assisted Solution

by:Manish
Manish earned 334 total points
ID: 33583370
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
 
LVL 92

Assisted Solution

by:objects
objects earned 166 total points
ID: 33583383
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
 

Author Comment

by:cofactor
ID: 33583487
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
Independent Software Vendors: 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!

 
LVL 11

Accepted Solution

by:
Manish earned 334 total points
ID: 33583715
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
 

Author Comment

by:cofactor
ID: 33584392
..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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36929492
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question