JPA 2 / 休眠孤儿删除仍然无法与@OneToMany?
我试图在Hibernate 4.3.5 / JPA2对象中使用orphanRemoval,但它似乎没有像我预期的那样工作。但是,我不确定我是否在做不正确的事情,或者这是否仍然是Hibernate中的一个错误。
给定以下关系(@Version,为简洁起见,省略了 getter 和 setters):
@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.contract 列表中删除一个协定,它将从协定表中删除相应的行,并从附件表中删除所有关联的附件。但是,只有协定表会被删除。不修改附件表。
前任:
// 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;
}
}
假设附件相对于“协定”表是 ManyToOne,如果删除了“协定”,则附件是孤立的。但即使使用 ,这也不会从 DB 中删除行。orphanRemoval=true
我发现Hibernate 3(在SO上,Jira和其他地方在线)发现了几个与此相关的问题,但我已经知道它在Hibernate 4中已修复。但是使用Hibernate 4.3.5我仍然看到这个问题。从这个问题来看,它似乎有效,所以我不确定为什么我不能让它正常工作。
我的代码中是否有错误/缺失,或者Hibernate仍然有问题?我是否需要在这些实体类中实现和实现才能正常工作?我尝试在合约和附件中实现这两种方法,但没有区别。equals
hashCode
orphanRemoval
查看 Hibernate 日志,它显示 Hibernate 对联接表(或 FK 映射)进行了更改,但实际上并没有从关联表中删除该行。我可以看到Hibernate在Contrace表中设置provider_id=null,但是它不应该删除Contrace行吗?
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]