Newsletter

Difference Between Hibernate Save And Persist Methods

Hibernate » on Sep 13, 2011 { 23 Comments } By Sivateja

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.

 

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

23 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.

    • priya srivastava says:

      lazy loading is used when your one entity class is having refrences of other POJO (or entity) like customer and address(address in customer)and if you fetch the data of customer,once you fire the query then the unusable data of addrress also get fetched up and get stored into the heap meomorey ..and you want nothing to do with the data of address then this is like wastage of memorey.
      so instead of fetching the level 2 data (address)lazy loading provides you to fetch the data of only first level(like customer id,customer name etc )but not address.once you request about the level 2 data like (query1.getaddresss()) then only the query will going to hit the datbase and get the result ..so it will help you in strong memorey manegmnet ..as when required then only the data get into the heap

  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. 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……..

  11. srinivas says:

    By default hibernate follows lazy loading for example if u want to retrive the record from database server when u are call the sessin.query() creare the all the object for parent table only if u use lazy=”false” it will be create parent table objects and child class objects

  12. Another difference between persist() and save(). persist() is JPA compliance, while save() is hibernate only.

    Persist() will consider JPAs Cascade.PERSIST, while Save() do not.

    Persist() will complaint if the an entity with UUID generator is set for it, and you try to persist an instance with ID already set. Some detached object exception. While save() will return the ID without complaint.

  13. Saikat says:

    please include the n+1 problem in hibernate

  14. santosh says:

    Does not hibernate make much confused by introducing persiste() and save() just with the return type? What if there were only one method which returns the ID?

  15. Jammulaiah says:

    Excellent work and explanation also very simple i never seen this much easy explanation in other portals.I love this website.

  16. RAVI TEJA RAVINOOTHALA says:

    excellent very helpful

  17. Chandra says:

    Siva, your explanation style is very nice.

  18. Geethanath says:

    Excellent work and good explanation. Loved your site!

  19. deepak says:

    How many ways we can get SessionFactory object in hibernate.

  20. Udaya says:

    Superb explanation sir…

  21. Harshal says:

    Superb explanation sir ji…

  22. Khushbu says:

    I am using hibernate persist method. When I run code from local I am getting error Ora: 014 cannot insert null while same code is working from Dev server. Imp point to mention am using same ds for local And Dev server.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.