Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @Where is not enforcing at @MappedSuperClass Entity

For migrating from eclipse link to hibernate, I am looking for the equivalent of eclipse link annotation @AdditionalCriteria in Hibernate at @MappedSupperClass BaseEntity level, to filter logically deleted records from all entities extending this BaseEntity.

I had found @Where annotation. But, this only works at Entity level and not at BaseEntity. Please let me know if there is any possibility to add this or any other Hibernate annotation to filter BaseEntity.

@MappedSuperclass
@Where(clause = "DEL_IND = 0")  // DOES NOT WORK
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "DEL_IND")
    private boolean deleted = Boolean.FALSE;

    public boolean getDeleted() {
        return deleted;
    }

    public void setDeleted() {
        this.deleted = Boolean.TRUE;
    }

}


@Entity
@Table(name = "PERSON")
@Where(clause = "DEL_IND = 0")  // THIS WORKS BUT NEEDS TO BE REPEATED IN ALL ENTITIES
public class Person extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name = "PERSON_ID")
    private Integer id;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "FIRST_NAME")
    private String firstName;

    --------------------
    getters & setters
    --------------------
    --------------------
}
like image 956
Bikram Avatar asked Sep 23 '14 03:09

Bikram


Video Answer


1 Answers

You can open a Hibernate JIRA issue for this. The only workaround is to manually add the @Where annotation to all your entities or use filters.

With filters you have the option of dynamically enable/disable them, which is useful since you might want to fetch a deleted item sometimes.

like image 189
Vlad Mihalcea Avatar answered Sep 30 '22 01:09

Vlad Mihalcea