{"id":599,"date":"2004-06-01T12:30:46","date_gmt":"2004-06-01T16:30:46","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=599"},"modified":"2004-06-01T12:30:46","modified_gmt":"2004-06-01T16:30:46","slug":"hibernate-deleting-objects","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/06\/01\/hibernate-deleting-objects\/","title":{"rendered":"Hibernate: Deleting Objects"},"content":{"rendered":"<p>It might look like this is turning into a Hibernate blog&#8230; maybe it&#8217;s just a phase I&#8217;m going through.  This last week I ran into a place in some code where I wanted to write a query to delete a number of rows from the database. Deleting a single row using Hibernate is simple; get an instance of the persistent object you want to delete (ie: a Book, etc..) and an instance of the <a href=\"http:\/\/www.hibernate.org\/hib_docs\/api\/net\/sf\/hibernate\/Session.html\">Session<\/a> object and then called the <a href=\"http:\/\/www.hibernate.org\/hib_docs\/api\/net\/sf\/hibernate\/Session.html#delete(java.lang.Object)\">delete(Object)<\/a> method:<br \/>\n<code><br \/>\nlong bookid = 12;<br \/>\nSession sess = HibernateFactory.currentSession();<br \/>\nObject book = sess.load(Book.class, bookid);<br \/>\nsess.delete(book);<br \/>\n<\/code><br \/>\nEasy.  Now, if you wanted to delete all the computer books for some reason, you&#8217;d have to first query the database using Hibernate to get a <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/util\/Collection.html\">Collection<\/a> of books, then iterate over the books, calling delete(Object object) on each one.<br \/>\n<code><br \/>\nCollection books = BookFactory.getComputerBooks();<br \/>\nSession sess = HibernateFactory.currentSession();<br \/>\nfor (Iterator i = books.iterator(); i.hasNext();) {<br \/>\n&nbsp;&nbsp;Book book = (Book)i.next();<br \/>\n&nbsp;&nbsp;sess.delete(book);<br \/>\n}<br \/>\n<\/code><br \/>\nIt&#8217;s easier than that though.  The Session object has a <a href=\"http:\/\/www.hibernate.org\/hib_docs\/api\/net\/sf\/hibernate\/Session.html#delete(java.lang.String)\">delete(String query)<\/a> method that I originally thought took a standard SQL &#8216;delete from table where&#8230;&#8217; statement. After fussing with it for a bit, I read the documentation which says: &#8220;Delete all objects returned by the query. Return the number of objects deleted.&#8221;. Aha! Instead of calling delete() on each one, you use a SQL (or a HQL) statement that fetches the objects and then pass the results directly to the delete method:<br \/>\n<code><br \/>\nSession sess = HibernateFactory.currentSession();<br \/>\nsess.delete(\"select book FROM org.mycompany.Book as book where type = 'technical'\");<br \/>\n<\/code><br \/>\nJust another example of how Hibernate makes it easier to work with relational databases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It might look like this is turning into a Hibernate blog&#8230; maybe it&#8217;s just a phase I&#8217;m going through. This last week I ran into a place in some code where I wanted to write a query to delete a number of rows from the database. Deleting a single row using Hibernate is simple; get &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/06\/01\/hibernate-deleting-objects\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hibernate: Deleting Objects<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/599"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}