Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:00:09+00:00 2026-06-11T13:00:09+00:00

I was looking for a way to avoid deleting my users from DB, but

  • 0

I was looking for a way to avoid deleting my users from DB, but instead to mark them as deleted and don’t bring them back in queries.

I found this plugin http://grails.org/plugin/hibernate-filter, which was a great tool for the task.

But when I tried to implement my solution, I passed trought same problems whose solutions wheren’t (or I was not able to find) on internet.

So, next, I describe the way that I solve the problem of soft delete.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:00:11+00:00Added an answer on June 11, 2026 at 1:00 pm

    In this example I will make my class User to handle it’s delete() method as a soft delete, setting the attribute lowDate with the actual date when delete() is called on an User instance. The idea is that users with lowDate != null will be ignored by GORM queries.

    1) Intall Hibernate Filter Plugin. Look for the dependency at the plugin’s page: http://grails.org/plugin/hibernate-filter. Take a look at the documentation.

    2) Add to Datasource the following:

    import org.grails.plugin.hibernate.filter.HibernateFilterDomainConfiguration
    
    environments {
        development {
        dataSource {
            ...    
            configClass = HibernateFilterDomainConfiguration
        }
        }
        test {
        dataSource {
            ...
            configClass = HibernateFilterDomainConfiguration
        }
        }
        production {
        dataSource {
            ...
            configClass = HibernateFilterDomainConfiguration
        }
        }   
    }
    

    3) Define your filter at the class:

    class User {
        ...
        String email
        Date lowDate
        static hibernateFilters = {
            deletedFilter(condition:'low_date is null', default:true)
        }
        static constraints = {
            ...
            lowDate nullable: true
        }
        ...
    }
    

    Note: look at the way I defined the condition. The value it receives is sql, so be careful to name the attribute as it is on the database instead of the name on the class.

    This will make GORM methods to avoid bringing users that have lowDate different than null.

    4) Define beforeDelele in a way to avoid phisical deletion:

    class User {
        ...
        def beforeDelete() {
            SecUser.executeUpdate("update SecUser su set lowDate = :lowDate where email = :email",
                                    [lowDate: new Date(), email: email])
            return false
        }
    }
    

    Note: I tried a simpler way to implement beforeDelete() that was

    def beforeDelete() {
        this.lowDate = new Date()
        this.save()
        return false
    }
    

    But when save() was called inside beforeDelete, the save method called beforeDelete, and so on, generating an StackOverflow. I don’t know why this happens.

    5) Enable the filter on BootStrap:

    class BootStrap {
        ...
        def init = { servletContext ->
            User.enableHibernateFilter('deletedFilter')
            environments {
                ...
            }
        }
    ...
    }
    

    That’s all, it show now work. To test the functionallity, here’s some sample spock tests:

    Note: ‘build’ method is from build-test-data plugin.

    class UserIntegrationSpec extends IntegrationSpec {
    
        def 'it should not find users marked as deleted'(){
            given: 'some users with lowDate and some withOut lowDate (=null)'
                User.build(firstName:'delUser1', lowDate: new Date())
                User.build(firstName:'user1')
                User.build(firstName:'delUser2', lowDate: new Date())
                User.build(firstName:'user2')
                def users = User.list()
            expect: 'it should only find the ones with lowDate == null'
                users.size() == 2
                users.every { it.firstName == 'user1' || it.firstName == 'user2' }      
        }
    
        def 'it should only delete users logically' (){
            given: 'a persisted user'
                def user = User.build(firstName: 'logiDelUser')
            when: 'user.delete() is called'
                user.delete(failOnError:true, flush:true)
                def deletedUser
                def users 
                User.withoutHibernateFilters(){
                    users = User.list()
                    deletedUser = User.find { firstName == 'logiDelUser' }
                }
            then: 'it should not delete the user from the DB, but set a low date instead'
                users.size() != 0
                deletedUser.lowDate != null
                deletedUser.firstName == 'logiDelUser' 
        }
    }
    

    Hope that that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking for a way to avoid starting from the head of the
I'm looking for a way to avoid declaring my configuration section in the configSections
I'm looking for a way to read specific files from a rar archive into
i'm looking for some way to avoid logging some specific files like /WebResource.axd and
just looking a way to avoid repeated post on my site, here is a
I'm looking for a way to avoid a form to be opened twice. Sometimes
I'm looking for a way to visually mark or tag a window (any OS)
I am looking for a way to create a ZIP file from a folder
I'm looking for way to create list view as like in IOS with pinned
I'm looking a way to enable IP logging with log4net in ASP.NET. I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.