Analytics

Saturday, August 1, 2009

Why Hibernate does "delete all then re-insert" - its not so strange

Overview

Hibernate allows developers to interact with data from a relational database in an object-oriented fashion. This brings a lot of benefits to maintenance of development. However, hibernate is an abstraction of data which essentially is its function as an ORM, and this usually forces the developer to know hibernate's internals in order to use the tool effectively.

Maintaining collections in the object world is something every application does. Using hibernate you can manipulate colllections by adding and deleting items through the java api, and by way of mapping, hibernate takes care of generating the SQL to augment that data in the database. If the nuances of the mapping is not understood, hibernate may produce what seems like strange results. A particular instance arises when you map a List on the many side of a one to many relationship. When removing an item from the list, hibernate issues a delete statement to delete all elements, and then issue insert statements to insert everything back in except the one removed. This seems ineffecient and odd. Why not just delete the one item? Then if you change the collection type to a Set, only one delete statement is issued by itself. This article tries to explain this behavior and hopefully bring to light hibernate's internals.


Explanation by Example

The scenario described above occurs when mapping a one to many relationship with a association table. The association table stores two attributes, each of which is a foreign key to another table. In addition, the many side object is mapped with a List, but no index column is used. Hibernate recommends when using a list that an index column is used, but why exactly? The example should illuminate why hibernate recommends the index column when mapping a list. The example will show you what happens when using a list without an index column.

Example Setup

Let's set up the example. Let's say I have two classes: A and B. There is a one to many relationship from A to B, where A has many B's in a List. Using hibernate we map B with one to many and leave out the index column.

As instances of these classes, we have an A1 with 3 children and A9 with no children (just for contrasting sake). The figure below illustrates this


In the database, we have three tables. The association table maintains the relationship between A and B. No constraints are in the table for simplicity sake. (We want to focus on hibernate's behavior and worry less about enforcing integrity in the database.) Notice there is not indexed column in AB.


Here is the data in the tables:

Also notice that the list contains two B1's, as a list is capable of containing duplicates. So we code in java to delete an element from A, say the first element in the list which is B1.



a.getBList().remove(0);


So what occurs on hibernate's flush? It first issues an sql to delete all rows in the relationship table where B_ID = 1. Then it inserts all the relationships that are not supposed to be deleted!
This seems strange.
However the reason why this occurs is because hibernate supports the additional feature of a collection that is called a Bag. A bag is a collection which can have duplicates but is unordered. The best feature of a bag is that you can get the number of occurrences of an object through the API with
public int occurrences(Object o) 
With a list, there is no way to do the same without iterating through the whole list.

Thus, when a list is mapped without an indexed collection, hibernate treats in like a bag. In essence, a list which has an indexed column is an "indexed bag.

Since hibernate handles our list as a bag, it believes there could be duplicate entries in the association table. In addition it knows the items in table AB are unordered, so there is no way to tell which row should be deleted from the object in java. We asked earlier, why not just issue one delete statement. If it stopped here we would end up with:


So hibernate needs to continue and re-insert the rows it knows from java that are not supposed to be deleted. In memory, it can just look at the list after remove() was called and insert the remaining relationships. So we end up with what we wanted:



Conclusion

When using a list in hibernate an indexed column is necessary if you want efficient manipulation of adding and deleting items. With an indexed column, when you code a remove(index), hibernate can relate that index to the column in the db and even if there are duplicates, it can distinguish and delete one row. Better yet, if duplicates are not needed in your application, you can use a Set. A set requires no index column and hibernate can distinguish one row because it know a set does not have duplicates.

If you are wondering what is the purpose of a Bag, references below may answer that question.

References

http://www.discursive.com/books/cjcook/reference/collections-sect-bag.html
http://guanyu.info/blog/?p=69
https://forum.hibernate.org/viewtopic.php?t=957657
http://sites.google.com/site/xushengxiaotech/Home/hibernate-tips--efficient-usage-of-bag-for-association

134 comments:

  1. This is the sort of things cam make life harder for DBAs with ORM database practises. If there is a requirement to maintain an audit trail then it gets polluted by masses of Delete/Insert pairs that effect no change but make reporting on time based views of the data histories a real pain.

    ReplyDelete
  2. That is a good point. ORMs definitely make java developers' lives easier by sparing them lots of yucky JDBC and mapping code. Using objects allows for modularity, encapsulation, and the whole OO works. However, if we don't know the internals of the ORM tool, we can run into issues you noted above.

    We can alleviate the mass delete issue by using an indexed column. I would hope a DBA would be willing enough to add such a column to service the application. The tradeoff's must be understood and should be well worth it.

    ReplyDelete
  3. Thanks! I had the same Problem. It deleted all and then inserted. To solve this,

    1) I declared the Collection as Set instead of List
    2) Overridden the hashCode and equals method

    It selectively deleted based on the removal in collection.

    The following blog also helped to achieve selective update in one-many relationship

    http://sieze.wordpress.com/2009/09/04/mapping-a-many-to-many-join-table-with-extra-column-using-jpa/#comment-51

    ReplyDelete
    Replies
    1. Oh, this link is off!!! I have just had this problem because server fell and hibernate deleted but not reinsert. I have lost all the registries. Who can i solve this for the next time? Thanks

      Delete
  4. thanks nirav. this is d information i wanted. u explained in such a simple n diagrammatic approach... thanks a lot... cleared my doubts...

    ReplyDelete
  5. Very nice explication! The only source I found where the reason is explained in detail. Thx!

    ReplyDelete
  6. Hi,
    You have explained it in detail with an example, you have not mentioned anything about Set.

    ReplyDelete
  7. Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training

    Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Java Online Training Java Online Training

    ReplyDelete
  8. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in dot net programming your post help me a lot.Thanks for your informative article.Dot Net training in chennai | dot net training and placement | Dot Net training in velachery

    ReplyDelete
  9. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Digital Marketing Training in Chennai

    Digital Marketing Training in Bangalore

    digital marketing training in tambaram

    digital marketing training in annanagar

    ReplyDelete
  10. I am definitely enjoying your website. You definitely have some great insight and great stories. 
    Blueprism online training

    Blue Prism Training in Pune

    ReplyDelete
  11. Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  12. myTectra a global learning solutions company helps transform people and organization to gain real, lasting benefits.Join Today.Ready to Unlock your Learning Potential !Read More...

    ReplyDelete
  13. Howdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?


    Best AWS Training Institute in BTM Layout Bangalore ,AWS Coursesin BTM


    Best AWS Training in Marathahalli | AWS Training in Marathahalli

    Amazon Web Services Training in Jaya Nagar | Best AWS Training in Jaya Nagar


    AWS Training in BTM Layout |Best AWS Training in BTM Layout

    AWS Training in Marathahalli | Best AWS Training in Marathahalli

    ReplyDelete
  14. Selenium is one of the most popular automated testing tool used to automate various types of applications. Selenium is a package of several testing tools designed in a way for to support and encourage automation testing of functional aspects of web-based applications and a wide range of browsers and platforms and for the same reason, it is referred to as a Suite.

    Selenium Interview Questions and Answers
    Javascript Interview Questions
    Human Resource (HR) Interview Questions

    ReplyDelete
  15. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    SEO Training near me
    SEO Training in Chennai
    SEO Training
    Digital Marketing Chennai
    Digital Marketing Courses in Chennai
    Digital marketing courses

    ReplyDelete
  16. Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
    Top college in Jaipur|law college|law college in Jaipur|Best law college in Jaipur

    ReplyDelete
  17. Wonderful blog.. Thanks for sharing informative Post. Its very useful to me.

    Guest posting sites
    Education

    ReplyDelete
  18. Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and
    keep blogging...

    bluecross
    Education

    ReplyDelete
  19. Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
    organic oil
    organic oil in jaipur
    organic cold pressed oils
    ayurvedic oil store in jaipur

    ReplyDelete
  20. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
    Best Devops Training in pune
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  21. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Devops Training courses
    python Training in chennai
    Devops Training in Bangalore

    ReplyDelete
  22. Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

    DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

    Good to learn about DevOps at this time.


    devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018

    ReplyDelete
  23. Such a wonderful blog on Machine learning . Your blog have almost full information about Machine learning .Your content covered full topics of Machine learning that it cover from basic to higher level content of Machine learning . Requesting you to please keep updating the data about Machine learning in upcoming time if there is some addition.
    Thanks and Regards,
    Machine learning tuition in chennai
    Machine learning workshops in chennai
    Machine learning training with certification in chennai

    ReplyDelete
  24. It's a good post and keep posting good article. it is very interesting to read.
    Regards,
    Devops Training Institute in Chennai

    ReplyDelete
  25. This is quite educational arrange. It has famous breeding about what I rarity to vouch.
    Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved.
    This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.


    Selenium interview questions and answers
    Selenium Online training
    Selenium training in Pune
    selenium training in USA
    selenium training in chennai

    ReplyDelete
  26. Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
    python Training institute in Chennai
    python Training institute in Bangalore
    python Training in Pune

    ReplyDelete
  27. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
    Thanks & Regards,
    VRIT Professionals,
    No.1 Leading Web Designing Training Institute In Chennai.

    And also those who are looking for
    Web Designing Training Institute in Chennai
    SEO Training Institute in Chennai
    Photoshop Training Institute in Chennai
    PHP & Mysql Training Institute in Chennai
    Android Training Institute in Chennai

    ReplyDelete
  28. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information regarding Microsoft Azure which is latest and newest,

    Regards,
    Ramya

    Azure Training in Chennai
    Azure Training Center in Chennai
    Best Azure Training in Chennai
    Azure Devops Training in Chenna
    Azure Training Institute in Chennai
    Azure Training in Chennai OMR
    Azure Training in Chennai Velachery
    Azure Online Training
    Azure Training in Chennai Credo Systemz

    ReplyDelete
  29. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  30. After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  31. This is the exact information I am been searching for, Thanks for sharing the required info with the clear update and required points. To appreciate this I like to share some useful information regarding This Topic which is the latest and newest for me.

    Regards,

    Whatsapp Group Links List

    ReplyDelete
  32. Thanks for posting such an blog it is really very informative. And useful for the freshers Keep posting the
    updates.

    Guest posting sites
    Technology

    ReplyDelete

  33. Nice Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Check out : best hadoop certification in chennai
    best hadoop training institute in chennai with placement
    best bigdata hadoop training in chennai
    big data training in chennai velachery

    ReplyDelete
  34. Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your knowledge.
    AWS Online Training
    AWS Training in Hyderabad
    Amazon Web Services Online Training

    ReplyDelete
  35. many peoples want to join random whatsapp groups . as per your demand we are ready to serve you whatsapp group links . On this website you can join unlimited groups . click and get unlimited whatsapp group links

    ReplyDelete
  36. if you want girls mobile numbers then this website is best for you . you can visit on this website and get their information and you also can meet with thrm and go for a date . click here to use our website --- online dating website

    ReplyDelete
  37. if you are searching for free unlimted tricks then visit now on Uvoffer.com and get unlimited offers and informations.
    film ka naam whatsapp puzzle answer film ka naam whatsapp puzzle

    ReplyDelete
  38. amazing blog layout! How long have you been blogging for? Get Free Girls WhatsApp Numbers Latest 2019 you make blogging look easy.

    ReplyDelete
  39. Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
    hadoop training in chennai cost
    hadoop certification training in chennai
    big data hadoop course in chennai with placement
    big data training in chennai

    ReplyDelete
  40. Really amazing article thanks for this article.

    ayushman mitra bharti

    ReplyDelete
  41. I Like the way you write a post so clean and good
    https://www.youtube.com/watch?v=J4psD3RXq68

    https://www.youtube.com/watch?v=jlL8s-wvhuE
    https://www.youtube.com/watch?v=D-QT3kk0lGk
    https://www.youtube.com/watch?v=bwxueYh605E
    https://www.youtube.com/watch?v=o6eMRHnGbEc

    ReplyDelete

  42. This information really amazing thanks for share..

    Baby Girl Names

    ReplyDelete
  43. Check the Karnataka results on the official website. Studenst click on the link given here to check the more info.

    ReplyDelete
  44. Official Tamil Nadu SSLC Result Portal's TN 10th Result March 2019 Check Online @ TNRESULTS.NIC.IN. The tnresults.nic.in 2019 SSLC results are an important part of the student's life.

    ReplyDelete
  45. Students can find their 2019 Maharashtra Board SSC Results, 2019 MSBSHSE 10th Class Results, Wise Name, Wise School, Toppers List etc. So bookmark and stay tuned to us on this web page.

    ReplyDelete
  46. Students can use the android app to check their 2019 Maharashtra SSC Result directly on their smartphone. Students are highly stressed about the outcome of the SSC, we are asked to be patient and visit our website for the latest news and updates regarding the 2019 SSC Result of the Maharashtra Board.

    ReplyDelete
  47. The Chattisgarh Board will also release the CGBSE class 12th result 2019 online on its official website for students ' convenience announcement. Once the results of the CGBSE class 12th are out, this page will be live here. Students will be able to check the results of CGBSE class 12th by following the procedure for checking the 2019 result of cgbse class 12th.

    ReplyDelete
  48. Students who give MP Board 12th Additional Exam 2019 will be able to check MPBSE 12th Additional Results 2019 on MP Board's official website.

    ReplyDelete
  49. Rajasthan Board 12th Results 2019 Name wise will be announced on May 22nd, 2019 on the official portal for the branch of Arts, Science and Commerce. Rajasthan RBSE Board Class 12th Time Table 2019 Download Rajasthan RBSE Board Senior Admit Card 2019 for Date Sheet 2019 12th Class.

    ReplyDelete
  50. You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!!
    digital marketing course
    www.excelr.com/digital-marketing-training

    ReplyDelete
  51. After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
    nice content on your website

    ReplyDelete
  52. Attend The Python Training in Hyderabad From ExcelR. Practical Python Training Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Hyderabad.
    python training in bangalore

    ReplyDelete
  53. This comment has been removed by the author.

    ReplyDelete
  54. TheDrive and its partners may earn a commission if you purchase a product through one of our links. Read more. A good dash cam makes it https://bestfrontandreardashcams.com/ and A driver has been charged after being caught on a truck's dash cam ... where the video shows the car repeatedly cutting in front of the truck and

    ReplyDelete

  55. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.

    Regrds,

    cloud computing courses in chennai | advanced java training institute in chennai | best j2ee training in chennai

    ReplyDelete

  56. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.

    Regrds,

    cloud computing courses in chennai | advanced java training institute in chennai | best j2ee training in chennai

    ReplyDelete

  57. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.

    Regrds,

    cloud computing courses in chennai | advanced java training institute in chennai | best j2ee training in chennai

    ReplyDelete
  58. Go Health Science is a best resource to get all kinds of Knowledge about Health and Science updates on Healthy Life ideas.

    ReplyDelete

  59. It’s interesting content and Great work. Definitely, it will be helpful for others. I would like to follow your blog. Keep post

    Check out:
    Selenium training courses in chennai
    Selenium training center in chennai
    Selenium training in chennai quora
    Selenium course fees in chennai

    ReplyDelete
  60. Nice Article ,keep it up ,It is very helpful Baddie Captions Check out.

    ReplyDelete
  61. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  62. I have gone through your blog post, nice blog it’s very helpful and I really thankful for you.
    we provide best services autocad classes in Bhopal as well as CPCT training in bhopal we provide service ,
    projector repairing also done by us.
    autocad in bhopal
    3ds max classes in bhopal
    CPCT Coaching in Bhopal
    java coaching in bhopal
    Autocad classes in bhopal
    Catia coaching in bhopal

    ReplyDelete
  63. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading Python classes in pune new articles. Keep up the good work!

    ReplyDelete
  64. Easily, the article is actually the best topic on this registry related issue. I fit in with your Python classes in pune conclusions and will eagerly look forward to your next updates.

    ReplyDelete
  65. Very interesting post this is my first time visit here.

    ReplyDelete
  66. วันที่มีความสุขและมีความสุข ขอบคุณมากสำหรับการแบ่งปันบทความ

    máy phun tinh dầu

    máy khuếch tán tinh dầu tphcm

    máy khuếch tán tinh dầu hà nội

    máy xông phòng ngủ

    ReplyDelete
  67. Attend The Digital Marketing courses in bangalore From ExcelR. Practical Digital Marketing courses in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Digital Marketing courses in bangalore.
    Digital Marketing Courses in Bangalore

    ReplyDelete