使用過濾器

@Filter 用作 WHERE 陣營,這裡有一些例子

學生實體

@Entity
@Table(name = "Student")
public class Student
{
    /*...*/

    @OneToMany
    @Filter(name = "active", condition = "EXISTS(SELECT * FROM Study s WHERE state = true and s.id = study_id)")
    Set<StudentStudy> studies;

    /* getters and setters methods */
}

研究實體

@Entity
@Table(name = "Study")
@FilterDef(name = "active")
@Filter(name = "active", condition="state = true")
public class Study
{
    /*...*/

    @OneToMany
    Set<StudentStudy> students;

    @Field
    boolean state;

    /* getters and setters methods */
}

StudentStudy 實體

@Entity
@Table(name = "StudentStudy")
@Filter(name = "active", condition = "EXISTS(SELECT * FROM Study s WHERE state = true and s.id = study_id)")
public class StudentStudy
{
    /*...*/

    @ManytoOne
    Student student;

    @ManytoOne
    Study study;

    /* getters and setters methods */
}

這樣,每次啟用活動過濾器時,

- 我們對學生實體進行的每個查詢都將返回所有學生只有他們的 state = true 學習

- 我們在 Study 實體上進行的每個查詢都將返回所有 state = true 研究

- 我們在 StudentStudy entiy 上進行的每個查詢都將返回具有 state = true 學習關係的查詢

請注意,study_id 是 sql StudentStudy 表上欄位的名稱