简体   繁体   English

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

[英]Multiple references to @OneToMany bag relationships using Hibernate

I have following (1:n) relationship: SchoolClass -> Students.我有以下 (1:n) 关系:SchoolClass -> Students。 SchoolClass can have multiple students, and Student can be assigned to only one SchoolClass . SchoolClass可以有多个学生,并且Student只能分配到一个SchoolClass In Hibernate ( SchoolClass class), I have following:在 Hibernate ( SchoolClass类) 中,我有以下内容:

    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;
    }

Now, I want to create another method, that lists all the Students of the SchoolClass (also the ones, that graduated with honours, so graduated_with_honors can be either 0 or 1 ).现在,我想创建另一种方法,列出 SchoolClass 的所有学生(还有那些以荣誉毕业的学生,因此graduated_with_honors可以是 0 或 1 )。 I have tried following:我试过以下:

    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;
    }

But this is not good approach, because now we have two collections that are modifying one table (it would throw hibernate found shared references to a collection exception) when used.但这不是一个好方法,因为现在我们有两个集合在使用时正在修改一个表(它会抛出hibernate found shared references to a collection异常的hibernate found shared references to a collection )。

Does anybody know how to do this?有人知道怎么做这个吗? Or, is there a way, how to insert a parameter into @hibernate.bag where clause , so we would change where clause according to the situation?或者,有没有办法,如何在@hibernate.bag where子句中插入参数,根据情况更改where子句?

Thanks in advance.提前致谢。

EDIT:编辑:

private transient List students;

- this have to stay the same, I have to keep it as it is. - 这必须保持不变,我必须保持原样。

There are many things that are wrong in your mapping:您的映射中有很多错误的地方:

  1. Collections should always be non-nullable:集合应始终不可为空:

     private List<Student> students = new ArrayList<>();
  2. Why do you use a Vector instead of a List ?为什么使用Vector而不是List The Vector is synchronized, while the ArrayList is not. Vector是同步的,而ArrayList不是。 Do you really want to use an entity concurrently?你真的想同时使用一个实体吗?

  3. You can't use the term class which is reserved in java, so maybe it's better to rename it to Course .您不能使用 java 中保留的术语class ,因此最好将其重命名为Course

  4. The graduated_with_honors relation can be best represented with a boolean in the Student class.Student类中可以用布尔值最好地表示graduated_with_honors关系。

     private boolean graduatedWithHonor;

Then, you can simply query all the Student(s) which have graduated with honor:然后,您可以简单地查询所有荣誉毕业的Student(s)

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM