Solved

merge in hibernate

Posted on 2010-09-01
7
701 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 59

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 40
Error in @AspectJ Based AOP with Spring 2 13
by zero exception 10 39
String array comparison 4 32
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now