从 grails 的排序集中删除所有项目

发布于 2024-09-27 12:12:10 字数 1410 浏览 5 评论 0原文

我有一个 grails 项目,其中有一个类,当我从控制器“手动”执行该类时,我可以毫无问题地删除该类。我使用以下代码。

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)
    }
}

这样就可以删除单个实例了。

现在我想从不同的控制器删除员工的所有项目。

它存储在员工中,如下所示:

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?

}

那么我现在如何从特定员工实例中删除所有项目?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瑶 2024-10-04 12:12:10

我可能会误解你的问题,因为我无法理解你的一些代码。看来没有必要。如果您的关系设置正确(即项目属于员工),这应该足以删除单个项目:

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)
}

如果这是一对多,则下次检索员工时该项目将消失。这应该可以删除员工的所有项目:

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)
}

假设级联:“all-delete-orphan”。如果不是这种情况,那么您可能还需要删除实例,这可能看起来像这样:

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)
}

我不是一个常规专家,所以不确定是否需要副本,或者是否可以直接迭代集合。似乎总有一种更绝妙的做事方式。您可能还想查看 deleteFrom 动态域类方法。这可能是一种更有效的 Grails 方法,具体取决于要删除的关系数量。

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.

小…楫夜泊 2024-10-04 12:12:10

您可以使用 Grails 生成的 removeFrom* 方法您声明了 hasMany 关系 - 它相当于 addTo*方法:

def employee = Employee.get(params.id)
employee.projects.toList().each { employee.removeFromProjects(it) } // toList() prevents a ConcurrentModifactionException

You could use the removeFrom* method that is generated by Grails when you declare the hasMany relationship - it's the equivalent of the addTo* methods:

def employee = Employee.get(params.id)
employee.projects.toList().each { employee.removeFromProjects(it) } // toList() prevents a ConcurrentModifactionException
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文