一尘不染

JPA 2 /Hibernate孤立删除仍然无法与@OneToMany一起使用?

hibernate

我正在尝试在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;
        }
    }

鉴于附件是相对于“合同”表的ManyToOne,如果删除了“合同”,则附件将被孤立。但是即使使用orphanRemoval=true,也不会从数据库中删除行。

我发现了一些与Hibernate 3有关的问题(都在SO,Jira和其他在线站点上),但是我已经了解到它在Hibernate4中已得到解决。但是在使用Hibernate4.3.5时,我仍然看到此问题。从此问题开始,它似乎可以正常工作,因此我不确定为什么无法使其正常运行。

我的代码中是否有错误/缺失,还是Hibernate仍然有问题?我是否需要实现equalshashCode在任何这些实体类中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]

阅读 220

收藏
2020-06-20

共1个答案

一尘不染

老实说,我不知道为什么,但是如果您向实体中的关系添加CascadeType.PERSIST(或更好CascadeType.ALL),它将按预期工作。@OneToMany``Provider

Hibernate文档可能缺少这一小细节。

*用JPA2 *更新 EclipseLink 2.5.1似乎没有这个问题

第二次更新

在第2.9节“实体关系”中,JPA 2.1规范指出:“如果孤立的实体是独立的,新的或已删除的实体,则orphanRemoval的语义不适用。”

我不知道您的相关实体是否分离,但是如果是的话,那不是一个bug :)

2020-06-20