Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

merge in hibernate

Posted on 2010-09-01
7
Medium Priority
?
711 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 1336 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 664 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Accepted Solution

by:
Manish earned 1336 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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