我正在使用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的删除似乎起作用。 我确实不想在用户小组中代表邀请,特别是因为归根结底,邀请将提及其他几个领域类别,删除其中任何一类,也应导致相应的邀请被删除。
我失踪了什么?
基因