Germán
Germán

Reputation: 328

Hibernate soft delete is not using @Where clause of relationships

I'm using hibernate in a Java/Spring/Struts2 project with soft delete. I have a relationship of two entities, and i expected that when i load the first entity from the DB, if the related one is deleted, it wont be loaded.

The Annotation of the First Entity is:

@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 {

This is the mapped relation

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

and the mapping of the second entity

@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

I've enabled the hibernate query log, and got this

 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=?

I am wrong expecting another "where" clause? Why hibernate is not using

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

Thank in advance, any idea will be welcome.

Upvotes: 1

Views: 3268

Answers (1)

MGorgon
MGorgon

Reputation: 2607

There is a bug / feature reported at hibernate bug tracker - it's from 2007:

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

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

@Where is working fine with @OneToMany, but it doesn't work with @ManyToOne.

Upvotes: 2

Related Questions