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

  • Home
  • SEARCH
  • 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 3279418
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:35:25+00:00 2026-05-17T19:35:25+00:00

I have a grails project with a class that I can delete no problem

  • 0

I have a grails project with a class that I can delete no problem when doing it “manually” from the controller. I use the following code.

def delete = {
    def projectInstance = Project.get( params.id )
    def employee = projectInstance.employee
    def projectarray = new ArrayList<Project>();
    projectarray += employee.getProjects()
    println("Size of projectarray is " + projectarray.size())
    if(projectInstance) {
        def rolearray = []

        projectarray.remove(projectInstance)
        def temp = new TreeSet<Project>();
        temp += employee.getProjects()
        temp.clear()
        temp.addAll(projectarray)
        employee.projects = temp

        projectInstance.employer = null
        projectInstance.delete(flush:true)
        flash.message = "Project ${params.id} deleted"
        redirect(action:"edit", controller: "employee", id: employee.id)
    }
    else {
        flash.message = "Project not found with id ${params.id}"
        redirect(action:list)
    }
}

So that deletes a single instance fine.

Now i want to, from a different controller, remove ALL projects from an employee.

This is stored in the employee like so:

class Employee implements Comparable
{
static hasMany = [projects:Project]  
static constraints = 
{
}

static mapping = {
    projects cascade:"all-delete-orphan", lazy:false
}

@XmlElementWrapper(name="projectslist")
SortedSet<Project> projects = new TreeSet<Project>();  // make a sortedSet?

}

So how would I now delete all projects from a particular employee instance?

  • 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-17T19:35:26+00:00Added an answer on May 17, 2026 at 7:35 pm

    I might be misunderstanding your question because I can’t make sense of some of your code. It seems unnecessary. If your relationships are setup correctly (i.e. Project belongsTo Employee), this should be sufficient to delete a single project:

    def delete = {
        def projectInstance = Project.get( params.id )
        projectInstance.delete(flush:true)
        flash.message = "Project ${params.id} deleted"
        redirect(action:"edit", controller: "employee", id: employee.id)
    }
    

    If this is a one-to-many, the next time you retrieve the employee the project will be gone. And this should work to delete all projects of an employee:

    def delete = {
        def employee = Employee.get( params.id )
        employee.getProjects().clear()
        employee.save(flash:true)
        flash.message = "All projects of employee deleted."
        redirect(action:"edit", controller: "employee", id: employee.id)
    }
    

    That assumes cascade:”all-delete-orphan”. If that’s not the case then you might need to also delete the instances and that might look something like this:

    def delete = {
        def employee = Employee.get( params.id )
    
        // Make copy to avoid concurrent modification issues later
        def copy = new TreeSet<Project>(employee.getProjects()); 
    
        employee.getProjects().clear();
        employee.save(flash:true)
    
        copy.each{
          $it.delete();
        }     
    
        flash.message = "All projects of employee deleted."
        redirect(action:"edit", controller: "employee", id: employee.id)
    }
    

    I’m not a groovy expert, so not sure if the copy is needed, or if you can just iterate on the collection directly. Seems like there is always a groovier way to do things. You might also want to check out the deleteFrom dynamic domain class method. That might be a more efficient grails approach depending on number of relationships to be deleted.

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

Sidebar

Related Questions

No related questions 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.