我有下面设置的项目。我正在尝试删除一个项目,并且得到以下信息:
2010-09-29 11:45:22,902 [http-8080-1] ERROR errors.GrailsExceptionResolver - deleted object would be re-saved by cascade (remove deleted object from associatio
ns): [Project#204]
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [Project#204]
at ProjectController$_closure6.doCall(ProjectController:50)
at ProjectController$_closure6.doCall(ProjectController)
at org.jsecurity.web.servlet.JSecurityFilter.doFilterInternal(JSecurityFilter.java:382)
at org.jsecurity.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:180)
at java.lang.Thread.run(Thread.java:619)
有什么办法让休眠告诉我哪个对象具有对要重新保存的Project对象的引用。我的项目设置有什么问题导致此循环异常?
public class Project implements Comparable
{
static belongsTo = [employee:Employee]
static hasMany = [roles:Role]
static constraints = {
}
static mapping = {
description type:"text"
roles lazy:false, cascade:"all,delete-orphan"
client lazy:false, cascade:"all,delete-orphan"
employer lazy:false, cascade:"all,delete-orphan"
}
}
class Employee implements Comparable
{
static hasMany = [employeeDesiredSkills:EmployeeDesiredSkill,
employeeDesiredTools:EmployeeDesiredTool,
employeeAreasOfExpertise:EmployeeAreaOfExpertise,
projects:Project,
education:Education]
static mapping = {
employeeDesiredSkills cascade:"all,delete-orphan", lazy:false
employeeDesiredTools cascade:"all,delete-orphan", lazy:false
education cascade:"all,delete-orphan", lazy:false
projects cascade:"all,delete-orphan", lazy:false
}
}
public class Role implements Comparable
{
static belongsTo = [project:Project]
static hasMany = [roleSkills:RoleSkill,roleTools:RoleTool]
static mapping = {
duties type:"text"
roleSkills cascade:"all,delete-orphan", lazy:false
roleTools cascade:"all,delete-orphan", lazy:false
locationType cascade:"all,delete-orphan", lazy:false
}
static contraints = {
endDate(nullable: true)
roleSkills(nullable: true)
roleTools(nullable: true)
}
}
class Employer implements Comparable
{
static mapping = {
type lazy:false, cascade:"all,delete-orphan"
}
}
class RoleTool implements Comparable
{
static belongsTo = [role:Role, tool:Tool]//not sure this makes sense, but have to leave it or DB breaks
Tool tool = new Tool(name:"")
static mapping = {
tool cascade:"save-update", lazy:false
}
class RoleSkill implements Comparable
{
static belongsTo = [role:Role, skill:Skill]//not sure this makes sense, but have to leave it or DB breaks
Skill skill = new Skill(name:"")
static mapping = {
skill cascade:"save-update", lazy:false
}
}
class Skill implements Comparable
{
static hasMany = [roleSkills:RoleSkill, employeeDesiredSkills:EmployeeDesiredSkill]
static mapping = {
roleSkills cascade:"all,delete-orphan", lazy:false
employeeDesiredSkills cascade:"all,delete-orphan", lazy:false
}
}
class Tool implements Comparable
{
static hasMany = [roleTools:RoleTool, employeeDesiredTools:EmployeeDesiredTool]
static mapping = {
roleTools cascade:"all,delete-orphan", lazy:false
employeeDesiredTools cascade:"all,delete-orphan", lazy:false
}
}
最佳答案
实际上,完成映射的方式projects cascade:"all,delete-orphan"
在Employee类中。
您只需要执行以下操作:
def employee = project.employee;
employee.removeFromProjects(project);
并且该项目也将被删除。
有关更多详细信息,请参考GORM Gotchas By Peter Ledbrook。
它分为三部分,介绍了许多类似的情况