HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

java-hibernate: Could you please explain the following questions?

1) What is the difference between get() and load() in hibernate?

2) what is the difference between the merge() and update() and join() methods in hibernate?

Explain the above mention concepts as early as possible.

Thanking you.

java x 210
hibernate x 23
Posted On : 2016-08-11 09:38:58.0


Difference between get() and load():
1) get() Returns null if object is not found, but load() Throws ObjectNotFoundException if object is not found.
2) get() method always hit the database. load() method doesn´t hit the database.
3) get() returns real object not proxy, while load() returns proxy object.
4) get() should be used if you are not sure about the existence of instance and load() should be used if you are sure that instance exists.

Difference between update() and merge():
update() and merge() both methods are used to convert the object which is in detached state into persistence state in hibernate. But there is a little difference. Let us see which method will be used in what situation.

Update():- update() is used to save the data when you are sure that the session does not contains an already persistent instance with the same identifier.

Merge():- merge() is used when you want to save your modifications at any time with out knowing about the state of a session.

Let Us Consider an Example:

1. ------
2. -----
3. SessionFactory factory = cfg.buildSessionFactory();
4. Session session1 = factory.openSession();
6. Student s1 = null;
7. Object o = session1.get(Student.class, new Integer(101));
8. s1 = (Student)o;
9. session1.close();
11. s1.setMarks(97);
13. Session session2 = factory.openSession();
14. Student s2 = null;
15. Object o1 = session2.get(Student.class, new Integer(101));
16. s2 = (Student)o1;
17. Transaction tx=session2.beginTransaction();
19. session2.merge(s1);

See from line numbers 6 to 9, we just loaded one object s1 into session1 cache and closed session1 at line number 9, so object s1 in the session1 cache will be destroyed as session1 cache will expires when ever we say session1.close()

Now s1 object will be in some RAM location, not in the session1 cache, here s1 is in detached state, and at line number 11 we modified that detached object s1, now if we call update() method then hibernate will throws an error, because we can update the object in the session only.

So we opened another session [session2] at line number 13, and again loaded the same student object from the database, but with name s2.

so in this session2, we called session2.merge(s1); now into s2 object s1 changes will be merged and saved into the database.

Posted On : 2016-08-11 21:50:09
Satisfied : 1 Yes  0 No
profile Rishi Kumar - Rishi Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -