我将项目设置如下。我想删除一个项目,我得到以下内容:
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)
有没有办法让hibernate告诉我哪个对象有引用回到将被重新保存的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
}
}
答案 0 :(得分:9)
事实上,你完成映射的方式
projects cascade:"all,delete-orphan"
在Employee类中。
你只需要这样做:
def employee = project.employee;
employee.removeFromProjects(project);
该项目也将被删除。 有关更多详细信息,请参阅GORM Gotchas By Peter Ledbrook。 它是一个3部分系列,并解释了许多类似的情况
答案 1 :(得分:5)
我知道已经晚了但有人可能想在这里找到这个: 会发生什么是Hibernate使用'dirtyChecking',如GORM Gotchas By Peter Ledbrook中所述。
简而言之:
Hibernate,所以Grails希望在Hibernate会话结束时save()
employee
对象(在.delete(flush:true)
调用的情况下),因为它检测到对象已被修改( project
被压制了。并且employee
必须保留与project
的链接,从而导致Hibernate感觉delete()
再次project
save()
。
要解决此问题:
def employee = project.employee;
employee.discard();
project.delete(flush:true);
如果没有你的要求,这就告诉Hibernate不要save()
employee
。
答案 2 :(得分:0)
如果你有删除设置的级联,就像你一样,你需要在删除它之前删除它的父对象。从第50行的ProcectController中的堆栈跟踪。