我正在尝试在 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;
}
我希望如果我从 Provider.contracts 列表中删除合同,它将从合同表中删除相应的行以及从附件表中删除所有关联的附件。但是,只有合同表被删除。附件表未修改。
前任:
// 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;
}
}
鉴于附件相对于合同表是多对一的,如果合同被删除,则附件是孤立的。但即使使用orphanRemoval=true
,这也不会从数据库中删除行。
我在 Hibernate 3 上发现了几个与此相关的问题(在 SO 和 Jira 和其他地方在线),但我知道它已在 Hibernate 4 中修复。但使用 Hibernate 4.3.5 我仍然看到这个问题。从这个问题,它似乎工作,所以我不知道为什么我不能让它发挥作用。
我的代码中是否存在错误/缺失,或者 Hibernate 仍然存在问题?我是否需要实现equals
并hashCode
在这些实体类中的任何一个中orphanRemoval
才能正常工作?我尝试在合同和附件中实现这两种方法,但没有任何区别。
查看 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]