Solved

merge in hibernate

Posted on 2010-09-01
7
698 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 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

15 Experts available now in Live!

Get 1:1 Help Now