使用过滤器

@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 表上字段的名称