Java 使用Hibernate多次引用@OneToMany包关系

Java 使用Hibernate多次引用@OneToMany包关系,java,hibernate,collections,persistence,Java,Hibernate,Collections,Persistence,我有以下(1:n)关系:班级->学生SchoolClass可以有多个学生,Student只能分配给一个SchoolClass。在Hibernate(SchoolClassclass)中,我有以下几点: private transient List students; /** * @return Returns students. * @hibernate.bag lazy="true" cascade="all" where="(graduated_with

我有以下(1:n)关系:班级->学生
SchoolClass
可以有多个学生,
Student
只能分配给一个
SchoolClass
。在Hibernate(
SchoolClass
class)中,我有以下几点:

    private transient List students;

    /**
     * @return Returns students.
     * @hibernate.bag lazy="true" cascade="all" where="(graduated_with_honors=0)" 
     * @hibernate.collection-key column="class_id"
     * @hibernate.collection-one-to-many class="my.project.namespace.Student"
     */
    public List getStudents() {
        if (students == null) {
            students = new Vector();
        }
        return students;
    }
    private transient List students, allStudents;

    /**
     * @return Returns students.
     * @hibernate.bag lazy="true" cascade="all" where="(graduated_with_honors=0)" 
     * @hibernate.collection-key column="class_id"
     * @hibernate.collection-one-to-many class="my.project.namespace.Student"
     */
    public List getStudents() {
        if (students == null) {
            students = new Vector();
        }
        return students;
    }

    /**
     * @return Returns students.
     * @hibernate.bag lazy="true" cascade="all" 
     * @hibernate.collection-key column="class_id"
     * @hibernate.collection-one-to-many class="my.project.namespace.Student"
     */
    public List getAllStudents() {
        if (allStudents == null) {
            allStudents = new Vector();
        }
        return allStudents;
    }
现在,我想创建另一个方法,列出该班级的所有学生(也包括那些以优异成绩毕业的学生,因此
以优异成绩毕业的学生可以是0或1)。我尝试过以下方法:

    private transient List students;

    /**
     * @return Returns students.
     * @hibernate.bag lazy="true" cascade="all" where="(graduated_with_honors=0)" 
     * @hibernate.collection-key column="class_id"
     * @hibernate.collection-one-to-many class="my.project.namespace.Student"
     */
    public List getStudents() {
        if (students == null) {
            students = new Vector();
        }
        return students;
    }
    private transient List students, allStudents;

    /**
     * @return Returns students.
     * @hibernate.bag lazy="true" cascade="all" where="(graduated_with_honors=0)" 
     * @hibernate.collection-key column="class_id"
     * @hibernate.collection-one-to-many class="my.project.namespace.Student"
     */
    public List getStudents() {
        if (students == null) {
            students = new Vector();
        }
        return students;
    }

    /**
     * @return Returns students.
     * @hibernate.bag lazy="true" cascade="all" 
     * @hibernate.collection-key column="class_id"
     * @hibernate.collection-one-to-many class="my.project.namespace.Student"
     */
    public List getAllStudents() {
        if (allStudents == null) {
            allStudents = new Vector();
        }
        return allStudents;
    }
但这不是一个好方法,因为现在我们有两个集合正在修改一个表(使用时会抛出对集合的共享引用)

有人知道怎么做吗?或者,有没有办法,如何在
@hibernate.bag where
子句中插入参数,以便我们根据情况更改
where
子句

提前谢谢

编辑:

private transient List students;

-这必须保持不变,我必须保持原样。

映射中有许多错误:

  • 集合应始终不可为空:

    private List<Student> students = new ArrayList<>();
    
  • 然后,您只需查询所有以荣誉毕业的
    学生

    Course course = ...;
    
    List<Student> honourableStudents = entityManager.createQuery(
            "select s " +
            "from Student s " +
            "where s.graduatedWithHonor = true " +
            "and s.course = :course", Student.class)
    .setParameter("course", course)
    .getResultList();
    
    Course=。。。;
    列出尊敬的学生=entityManager.createQuery(
    “选择s”+
    “来自学生s”+
    “其中s.graduatedWithHonor=true”+
    “和s.course=:course”,学生,班级)
    .setParameter(“课程”,课程)
    .getResultList();
    
    我已将班级更名为SchoolClass(这只是一个简化的示例,所以我不明智地使用了班级,对此表示抱歉)。我必须使用向量,这部分代码我无法更改。我明白了。不过,您可以使用布尔标志。