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 7439235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:40:36+00:00 2026-05-29T10:40:36+00:00

i have the following (simplified) domain classes class Filter { String name static hasMany

  • 0

i have the following (simplified) domain classes

class Filter {
    String name
    static hasMany = [answers:Answer]
    static belongsTo = [user:User]
}

class User {
    String name
    static hasMany = [answers:Answer, filters:Filter]
}

class Answer {
    String text
}

Then i add answers to the user which is working perfectly. The problem occurs when i delete 1 answer of a user:

def delete = {
    def answer = Answer.get(params.id)
    def users = User.withCriteria() {
        answers{
            eq("id", answer.id)
        }
    }

    for (user in users)
        user.removeFromAnswers(answer)

    answer.delete(flush:true)
    redirect(action:"index")
}

What happens here is that ALL user –> answer associations get deleted.

I only want to delete this 1 answer and of cause all associations the answer is used.

I know this has to do with the missing belongsTo, but i can’t use it because a ansswer can either belong to a user or to an filter…

  • 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-05-29T10:40:37+00:00Added an answer on May 29, 2026 at 10:40 am

    You can add the belongsTo to set them to nullable:

    class Answer {
        String text
    
        static belongsTo = [user:User, filter:Filter]
        static constraints = {
            user nullable:true
            filter nullable:true
        }
    }
    

    and then just delete the Answer directly in the Controller:

    def delete = {
        def answer = Answer.get(params.id)
        answer.delete(flush:true)
    }
    

    GORM will take care of the rest the cascading for you.

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

Sidebar

Related Questions

As a simplified example, I have the following data classes: public class Employee {
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package
I have the following code(simplified). public class OrderProcessor { public virtual string PlaceOrder(string test)
I have the following (greatly simplified) classes: public class Customer { [Required(ErrorMessageResourceName=Required, ErrorMessageResourceType=typeof(ResourcesCommon.ValidationStrings))] public
I have the following (simplified) method: private static string GetStringFromValue<T>(T val) { if (typeof(T)
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I am using the SQLite database and have the following persistent class (simplified): public
I have the following methods in an enum helper class (I have simplified it
I have the following setup (simplified, obviously): An abstract class, with an A object
I have the following 4 tables (simplified for this question): Company CompanyId | Name

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.