8 Replies Latest reply on Sep 29, 2010 7:21 AM by iravhari

    How to function soft delete

    vasukihn

      Hello All,


      I am trying to have soft delete feature in my application. Let me explain you my scenario


      1. I have a table named site with a column active and many other columns.
      2. The respective entity Site.java is created.


      Now I will Lists the site which are active in the data-grid. i have delete link for each row in a data-grid. upon clicking on any of the row's delete link, the active column in the site table must become false.


      How do i do this? Please help me in explaining the steps to carry out this .


      Thanks
      Vasuki

        • 1. Re: How to function soft delete
          boomerbrian.brian.abston.featurenotbug.com

          I blogged about soft deletes a couple of months ago. This shows you how to do it.


          Soft Deletes Using Hibernate

          • 2. Re: How to function soft delete
            vasukihn
            Hello Brain,

            Thanks for replying.

            I read through your blog. After reading your posting i got to know that we can make use of hibernate annotations for soft delete. I did the same changes as you have explained in my java file. But i'm getting many errors. So i thought i should also change in *.xhtml file. If so, what changes i need to do? please explain me detail.
            • 3. Re: How to function soft delete
              vasukihn

              Sorry Brian. i misspelled your name. Please let me know the solution.

              • 4. Re: How to function soft delete
                boomerbrian.brian.abston.featurenotbug.com

                You shouldn't have to change anything in the view. The soft delete is handled by Hibernate. The annotations should be all you need. The view and controller layer knows nothing about it.


                Post your code and errors in this thread.

                • 5. Re: How to function soft delete
                  vasukihn
                  Hello Brian,

                  Thanks for writing the blog. Earlier i followed the blog and made the same changes in my model.
                  The reason why I was getting the error message was, while writing the query

                  (@SQLDelete(sql="UPDATE site SET active= '0' WHERE locationid = ?"))

                  I mistyped the column name (locationid )so it was saying "Coudln't identify the column in the database".
                  Now i changed the column name and soft delete is happening.

                  Now i have another question. How to soft delete the child table data while deleting the parent table data?
                  Example:

                  Site is a parent table and Location is the child table for it. When i do soft delete for a row in site table the associated row in the location table must also be inactivated.
                  So please guide me how to do this.


                  Thanks
                  Vasuki
                  • 6. Re: How to function soft delete
                    boomerbrian.brian.abston.featurenotbug.com

                    Setup your Location Entity to soft delete just like you did with the Site Entity. Then you will need to set the Location relationship in the Site Entity to cascade delete. Here is an example. Look at the Hibernate docs for all of the cascade options. Now when you delete the Site the Location should be deleted as well.




                    @OneToMany(fetch = FetchType.LAZY, mappedBy = "site", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
                      public Set<Location> getLocations() {
                      return this.Locations;
                    }





                    • 7. Re: How to function soft delete
                      vasukihn

                      Hello Brian,


                      Thanks for the solution. It is working like a charm.

                      • 8. Re: How to function soft delete
                        iravhari

                        Thanks for the valuable update Brain.


                        Hi vasuki,
                             Have you tried the same soft delete for ManyToMany relationship where the reference table doesn't have an entity?


                             What needs to be modified on the code