【发布时间】:2010-01-29 09:02:15
【问题描述】:
我尝试使用级联“remove”(jpa)和“delete-orphan”映射一对多关系,因为我不希望在保存或保留父级时保存或保留子级(安全由于客户端到服务器(GWT,Gilead)的原因)
但是这个配置不起作用。当我尝试使用级联“全部”时,它会运行。为什么 delete-orphan 选项需要级联“all”才能运行?
这里是代码(为简单起见,没有id或其他字段,类Thread定义了一个简单的多对一属性,没有级联):
在事务函数中使用removeThread 函数时,它不会运行,但如果我将cascade.Remove 编辑为cascade.All,它会运行。
@Entity
public class Forum
{
private List<ForumThread> threads;
/**
* @return the topics
*/
@OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public List<ForumThread> getThreads()
{
return threads;
}
/**
* @param topics the topics to set
*/
public void setThreads(List<ForumThread> threads)
{
this.threads = threads;
}
public void addThread(ForumThread thread)
{
getThreads().add(thread);
thread.setParent(this);
}
public void removeThread(ForumThread thread)
{
getThreads().remove(thread);
}
}
【问题讨论】: