我试图在Hibernate 4.3.5/JPA2对象中使用orphanRemoval,但它似乎不像我预期的那样工作。我不确定,但是,如果我做错了什么,或者如果这仍然是一个错误在Hibernate。
给定以下关系(@Version、getter和setter为简洁省略):
@Entity
public class Provider implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
private String name;
@OneToMany(orphanRemoval=true,cascade=CascadeType.REMOVE)
@JoinColumn(name="provider_id", referencedColumnName="id")
private List<Contract> contracts;
}
@Entity
public class Contract implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
private String volume;
@OneToMany(orphanRemoval=true,cascade=CascadeType.REMOVE) // delete any attachments that were previously uploaded with this contract
@JoinTable(name="contract_attachment", joinColumns = @JoinColumn(name = "contract_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "attachment_id", referencedColumnName = "id"))
private List<Attachment> attachments;
}
@Entity
public class Attachment implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
private String filename;
}
如果我从提供程序中删除一个合同,我将期望。合同列表,它将从合同表中删除相应的行,并从附件表中删除所有相关的附件。然而,只有契约表被删除。附件表未修改
,
// loop over all contracts and delete the one with the matching id
for(Iterator<Contract> it = provider.getContracts().iterator(); it.hasNext();){
Contract c = it.next();
if( c.getId() == contractId ){
it.remove();
break;
}
}
假设附件相对于Contract表是ManyToOne,如果删除Contract,则附件是孤立的。但是,即使使用orphanRemoval=true
,这也不会从DB中删除行。
我在Hibernate 3中发现了几个与此相关的问题(包括在SO, Jira和其他在线网站上),但我知道它在Hibernate 4中已经修复了。但是在使用Hibernate 4.3.5时,我仍然看到这个问题。从这个问题来看,它似乎可以工作,所以我不确定为什么我不能让它工作。
我的代码中是否有错误/缺失,或者Hibernate仍然有问题?我是否需要在orphanRemoval
的任何这些实体类中实现equals
和hashCode
才能正常工作?我尝试在Contract和Attachment中实现这两种方法,但是没有任何区别。
查看Hibernate日志,它显示Hibernate对连接表(或FK映射)进行了更改,但实际上并没有从关联表中删除该行。我可以看到Hibernate在Contract表中设置provider_id=null,但它不应该删除Contract行吗?
2014-07-04 15:06:41,333 [main] [-] DEBUG org.hibernate.SQL -
/* update
com.ia.domain.Provider */ update
provider
set
default_contact_id=?,
name=?,
type=?,
version=?,
website=?
where
id=?
and version=?
Hibernate:
/* update
com.ia.domain.Provider */ update
provider
set
default_contact_id=?,
name=?,
type=?,
version=?,
website=?
where
id=?
and version=?
2014-07-04 15:06:41,334 [main] [-] TRACE hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [null]
2014-07-04 15:06:41,334 [main] [-] TRACE hibernate.type.descriptor.sql.BasicBinder - binding parameter [2] as [VARCHAR] - [name_3]
2014-07-04 15:06:41,335 [main] [-] TRACE org.hibernate.type.EnumType - Binding [CARRIER] to parameter: [3]
2014-07-04 15:06:41,336 [main] [-] TRACE hibernate.type.descriptor.sql.BasicBinder - binding parameter [4] as [INTEGER] - [2]
2014-07-04 15:06:41,336 [main] [-] TRACE hibernate.type.descriptor.sql.BasicBinder - binding parameter [5] as [VARCHAR] - [website_3]
2014-07-04 15:06:41,337 [main] [-] TRACE hibernate.type.descriptor.sql.BasicBinder - binding parameter [6] as [BIGINT] - [4]
2014-07-04 15:06:41,338 [main] [-] TRACE hibernate.type.descriptor.sql.BasicBinder - binding parameter [7] as [INTEGER] - [1]
2014-07-04 15:06:41,342 [main] [-] DEBUG org.hibernate.SQL -
/* delete one-to-many com.ia.domain.Provider.contracts */ update
contract
set
provider_id=null
where
provider_id=?
Hibernate:
/* delete one-to-many com.ia.domain.Provider.contracts */ update
contract
set
provider_id=null
where
provider_id=?
2014-07-04 15:06:41,344 [main] [-] TRACE hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [4]
老实说,我不知道为什么,但是如果你在Provider
实体中将CascadeType.PERSIST
(或更好的CascadeType.ALL
)添加到@OneToMany
关系中,它将按预期工作。
可能Hibernate文档缺少这一点细节。
EclipseLink 2.5.1与JPA2似乎没有这个问题
2日更新
在JPA 2.1规范的第2.9节,实体关系中说:如果被孤立的实体是分离的、新的或已删除的实体,则不适用orphanRemoval的语义。
我不知道你的相关实体是否分离,但如果是,那么它不是一个bug:)
我也得到的问题说明。虽然它已被弃用,但下面的使用可以很好地删除孤儿:
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)