{"id":3611,"date":"2011-04-14T21:03:57","date_gmt":"2011-04-14T15:33:57","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3611"},"modified":"2012-06-29T15:01:10","modified_gmt":"2012-06-29T09:31:10","slug":"truncate-table-executeupdate-or-createsqlquery","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/truncate-table-executeupdate-or-createsqlquery\/","title":{"rendered":"Truncate Table ? executeUpdate OR createSQLQuery"},"content":{"rendered":"<p>In one of the modules that I was working on , I needed to delete the previous data from a table then load up the data into those tables again and then manipulate code through the &#8216;id&#8217;s&#8217; on those tables.<br \/>\nSo this is what I was doing &#8230;<\/p>\n<p>[groovy]<br \/>\nEvent.executeUpdate(&#8216;delete from Event&#8217;)<br \/>\nEventInstance.executeUpdate(&#8216;delete from EventInstance&#8217;)<br \/>\n[\/groovy]<br \/>\n<\/br><br \/>\nBut the <strong>problem<\/strong> was that when I deleted the events, the &#8216;id&#8217;s&#8217; on those tables would remain intact and the new Events that would load up would continue upon the earlier present &#8216; id&#8217;s &#8216; .i.e if the last present id was 24 before deleting , the new load-ups would continue from 25 onwards..<\/p>\n<p>This made realise that truncate was a better option &#8230; But <strong>unfortunately<\/strong> the GORM layer does not support the &#8216; truncate &#8216; queries that I was looking for.<br \/>\nSo in effect this query would give an error<br \/>\n<\/br><br \/>\n[groovy]<br \/>\nEvent.executeUpdate(&#8216;truncate table Event&#8217;)<br \/>\nEventInstance.executeUpdate(&#8216;truncate table EventInstance&#8217;)<br \/>\n[\/groovy]<\/p>\n<p>So with some help from &#8216; Mr. Google &#8216; and my fellow colleagues , i realised that there was another way to do this ..<br \/>\n<strong> Session Factory &#8230;<\/strong><br \/>\nSession Factory allowed me to truncate those tables through\u00a0 createSQLQuery() method<br \/>\nSo my new approach was<br \/>\n<\/br><\/p>\n<p>[groovy]<br \/>\n   def sessionFactory<\/p>\n<p>   def session = sessionFactory.getCurrentSession()<br \/>\n   Query query = session.createSQLQuery(&#8216;truncate table event&#8217;)<br \/>\n[\/groovy]<\/p>\n<p>The<strong> getCurrentSession()<\/strong> method looks at the current context to see if a session is stored in there. If there is one, it uses it, and if there isn&#8217;t a session in the current context, it creates a new one and saves it in there.<br \/>\nThen all I did was use the query to truncate the tables&#8230; and presto !! .. my work was done ..<br \/>\n<\/br><\/p>\n<p>Hope this helps .. \ud83d\ude00<\/p>\n<p>Cheers<\/p>\n<p>Manoj Mohan<br \/>\nManoj (at) Intelligrape (dot) com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of the modules that I was working on , I needed to delete the previous data from a table then load up the data into those tables again and then manipulate code through the &#8216;id&#8217;s&#8217; on those tables. So this is what I was doing &#8230; [groovy] Event.executeUpdate(&#8216;delete from Event&#8217;) EventInstance.executeUpdate(&#8216;delete from EventInstance&#8217;) [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[4843,844,4840,843],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3611"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3611"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3611\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}