English 中文(简体)
停泊在铁路中删除无双向关系
原标题:Cascading deletes without bidirectional relationships in Grails

我正在使用1.3.7号铁路,并且有以下领域:

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

在删除用户小组案例时,我谨删除提及用户小组的所有邀请事例,而没有提及用户小组邀请的明确关系。 换言之,我要在不修改小组的情况下,从用户小组暂时删除。

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

当我接受考验时,它就失败了:

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

看来,这似乎像直言不相干的事例,但 cas的删除似乎起作用。 我确实不想在用户小组中代表邀请,特别是因为归根结底,邀请将提及其他几个领域类别,删除其中任何一类,也应导致相应的邀请被删除。

我失踪了什么?

基因

最佳回答

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

那么,你的测试就变成了。

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

几个无关的小笔记——财产因缺损而无损,因此,你可以取消<代码>组(无法计算:虚假)限制。 如你界定了该名称的变数,则在地图上打上belongs To。 用户组,<编码>。

最新资料:

另一种不太侵扰的办法是在<代码>上使用前列活动。 用户组:

def beforeDelete() {
   Invitation.executeUpdate(
       delete from Invitation where group=:group ,
      [group: this])
}
问题回答

暂无回答




相关问题
grails + gwt request handling via controllers

I am new to gwt. I am trying to integrate gwt+grails.Can anybody provide me a good example for handling the request using grails controllers and not a custom servlet.I will be really thankful if ...

Error loading the grails gwt module xml

I ve installed the plugin from this article by Peter http://www.cacoethes.co.uk/blog/groovyandgrails/the-command-pattern-w.... While compile time its not able to find the module file which is present ...

Sorting Objects Based on Custom Domain Class Methods

I have a domain class, in which I ve defined some methods which give the object a score based on different algorithms (eg. popularity). I now want to retrieve a list of these objects sorted by one of ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

热门标签