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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:06:05+00:00 2026-05-24T03:06:05+00:00

I am using Grails 1.3.7, and have the following domain classes: package com.fxpal.test class

  • 0

I am using Grails 1.3.7, and have the following domain classes:

package com.fxpal.test

class UserGroup {

    String name

    static constraints = {
        name(blank: false)
    }
}

class Invitation {

    UserGroup group
    String user

    static belongsTo = [group: UserGroup]

    static constraints = {
        group(nullable: false)
    }
}

I would like to be able to delete all Invitation instances that refer to a UserGroup instance when that UserGroup instance is deleted, without having an explicit relationship that refers to Invitation in UserGroup. In other words, I would like to have a cascading delete from UserGroup to Invitation, without modifying Group.

My test for this fails due to a constraint that represents the Invitation -> UserGroup relationship:

void testCascadingDelete() {
    UserGroup group1 = new UserGroup(name: 'group1').save(flush: true, failOnError: true)
    UserGroup group2 = new UserGroup(name: 'group2').save(flush: true, failOnError: true)

    Invitation invitation = new Invitation(user:'user1', group: group1).save(flush: true, failOnError: true)

    assertEquals("Wrong number of groups.", 2, UserGroup.count())
    assertEquals("Wrong number of invitations.", 1, Invitation.count())

    group1.delete(flush: true, failOnError: true)

    assertEquals("Wrong number of groups.", 1, UserGroup.count())
    assertEquals("Wrong number of invitations.", 0, Invitation.count())
}

When I run the test, it fails like this:

could not delete: [com.fxpal.test.UserGroup#1]; SQL [delete from user_group where id=? and version=?]; constraint [FK473F7799A8642225]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [com.fxpal.test.UserGroup#1]
org.springframework.dao.DataIntegrityViolationException: could not delete: [com.fxpal.test.UserGroup#1]; SQL [delete from user_group where id=? and version=?]; constraint [FK473F7799A8642225]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [com.fxpal.test.UserGroup#1]
    at com.fxpal.test.InvitationIntegrationTests.testCascadingDelete(InvitationIntegrationTests.groovy:23)
Caused by: org.hibernate.exception.ConstraintViolationException: could not delete: [com.fxpal.test.UserGroup#1]
    at com.fxpal.test.InvitationIntegrationTests.testCascadingDelete(InvitationIntegrationTests.groovy:23)
  ...

It seems like the canonical nose-face example, but the cascading delete doesn’t seem to work. I really don’t want to have to represent the Invitation in the UserGroup, in particular because in the end, the Invitation will reference several other domain classes, the deletion of any of which should cause the corresponding Invitation to get deleted as well.

What am I missing?

Gene

  • 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-24T03:06:05+00:00Added an answer on May 24, 2026 at 3:06 am

    I’m not sure if it’s possible without the bidirectional relationship, but you can easily do it yourself in a transactional service method (to make sure the deletes all happen or none happen):

    void deleteGroup(UserGroup group) {
       Invitation.executeUpdate(
           'delete from Invitation where group=:group',
           [group: group])
       group.delete(flush: true, failOnError: true)
    }
    

    and then your test becomes

    def fooService
    
    ...
    
    void testCascadingDelete() {
        UserGroup group1 = new UserGroup(name: 'group1').save(flush: true, failOnError: true)
        UserGroup group2 = new UserGroup(name: 'group2').save(flush: true, failOnError: true)
    
        Invitation invitation = new Invitation(user:'user1', group: group1).save(flush: true, failOnError: true)
    
        assertEquals("Wrong number of groups.", 2, UserGroup.count())
        assertEquals("Wrong number of invitations.", 1, Invitation.count())
    
        fooService.deleteGroup group1
    
        assertEquals("Wrong number of groups.", 1, UserGroup.count())
        assertEquals("Wrong number of invitations.", 0, Invitation.count())
    }
    

    A couple of minor unrelated notes – properties are not-null by default, so you can remove the group(nullable: false) constraint. And a belongsTo in Map form like you have defines a variable of that name, so you can omit UserGroup group in Invitation.

    UPDATE:

    Another less intrusive option is to use the before-delete event in UserGroup:

    def beforeDelete() {
       Invitation.executeUpdate(
          'delete from Invitation where group=:group',
          [group: this])
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grails domain class Character class Character { String name int level
Grails question: Confused about using a package to hold domain classes. I'm using Netbeans
All, I am trying to test my grails app using Hudson. I have in
I have the following UrlMapping in my Grails UrlMappings class: /$something/ { controller =
I'm using Grails, and I have a domain model with multiple hasMany attributes to
I'm considering using Grails for my current project. I have a couple of requirements
I'm following the racetrack example from Jason Rudolph's book at InfoQ , using grails-1.2.1.
According to Grails API, we can retrieve all domain classes definition through GrailsApplication as
Using Grails and CXF, I have published a small web service that looks like
We're developing an app (using Grails Spring Security (formerly Acegi)) in which we'll have

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.