Hibernate软删除不使用@Where子句关系

时间:2012-03-15 21:12:41

标签: hibernate jpa annotations

我在Java / Spring / Struts2项目中使用hibernate进行软删除。我有两个实体的关系,我期望当我从数据库加载第一个实体时,如果相关的实体被删除,它就不会被加载。

第一个实体的注释是:

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "users")
@SQLDelete(sql = "UPDATE users SET active = '0' WHERE id_user = ?")
@Where(clause = "active = '1'")
public class User {

这是映射关系

@Cascade({ CascadeType.ALL, CascadeType.DELETE_ORPHAN })
@ManyToOne
@JoinColumn(name = "id_social_data")
@Where(clause = "active = '1'")
public SocialData getSocialData() {
    return socialData;
}

和第二个实体的映射

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "social_datas")
@SQLDelete(sql = "UPDATE social_datas SET active = '0' WHERE id_social_data = ?")
@Where(clause = "active = '1'")
public class SocialData

我启用了hibernate查询日志,并得到了这个

 select
    this_.id_user as id1_24_8_,
    this_.active as active24_8_,
.....
    socialdata5_.id_social_data as id1_18_3_,
    socialdata5_.active as active18_3_,
    .....
from
    users this_ 
    .....

left outer join
    social_datas socialdata5_ 
        on this_.id_social_data=socialdata5_.id_social_data 
    .....

where
    (
        this_.active = '1'
    ) 
    and this_.username=? 
    and this_.password=?

我错了,期待另一个“where”条款?为什么hibernate没有使用

where
    (
        this_.active = '1'
        and socialdata5_.active = '1'
    ) 

提前感谢,欢迎任何想法。

1 个答案:

答案 0 :(得分:1)

hibernate bug跟踪器报告了一个错误/功能 - 它来自2007年:

https://hibernate.atlassian.net/browse/HHH-2737

https://hibernate.atlassian.net/browse/HHH-4335

@Where与@OneToMany合作正常,但它不能与@ManyToOne一起使用。