Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Difference Between Hibernate Save And Persist Methods

Hibernate » On Sep 13, 2011 | { 10 Comments }

Actually the difference between hibernate save() and persist() methods is depends on generator class we are using.

  • If our generator class is assigned, then there is no difference between save() and persist() methods. Because generator ‘assigned’ means, as  a programmer we need to give the primary key value to save in the database right [ Hope you know this generators concept ]
  • In case of other than assigned generator class, suppose if our generator class name is Increment means hibernate it self will assign the primary key id value into the database right [ other than assigned generator, hibernate only used to take care the primary key id value remember :-) ], so in this case if we call save() or persist() method then it will insert the record into the database normally

But here thing is,  save() method can return that primary key id value which is generated by hibernate and we can see it by

long s = session.save(k);

In this same case, persist() will never give any value back to the client, hope you are clear.

 

 

What you are thinkig....

10 Responses to “Difference Between Hibernate Save And Persist Methods”
  1. Mohammed Vaseem says:

    Hello java4s,
    Please provide the concepts of lazy loading and where it is used in real time applications.
    Thanks in advance.

  2. Java4s says:

    @Vaseem

    Sure,i do accept !
    Actually we ignored few topics, which are very important in the interview point of view.

    We noted this for an update, thank you.

  3. kand says:

    I just checked session.save() method is not returning any primary key.
    I think the difference lies with rest to transaction context.

  4. Java4s says:

    @kand

    Its depends on the generator we are using, it wont gives any return value back to assigned.

    - Comment edited (Reason : corrected)

  5. suresh says:

    Hello sir,
    what is difference between load() &get() in hibernate?

  6. Naga says:

    @suresh:
    load is the lazy function
    when doing emp e1 = session.load()… query doesnt happen .. it happen only when get the value ie. emp.getName();

    but in session.get().. query happens that time itself..

  7. Rohit Jain43 says:

    Hello Sir,

    Thanks for the info provided.
    If you perform save and persist for the same object at same time (one after the another) only one object is saved into the DB.

    For e.g. : Person p = new Person();
    p.setName(“dfdfd”);
    session.save(p);
    session.persist(p);

  8. Superb example I just danced with joy after reading the example. You have cleared the point. this example is direct hit to the target like bullet fired at correct target with sniper gun not like typical example stuffs that says bluh bluh bluh bluh bluh and bluh… and got just confused.

  9. swapna says:

    hibernate generators are clearly explained very nice article

  10. Rajesh says:

    super explaination sir……..

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.