简体   繁体   中英

How to fetch list using Criteria Many to Many unidirectional association in hibernate?

I have two Entity classes which possess many to many uni directional association.Here is my full code. What would be query if I want to fetch list of students which have same course like English ? I have no getter setter of list of student in Courses Entity because I am using unidirectional many to many relationship . Please help me.

@Entity
    @Table(name = "course")
    public class Courses {
        @Id
        @Column(name = "sid")
        @GeneratedValue(generator = "uuid")
        private String id;

         //other getters setters
    }


    @Entity
    @Table(name = "student")
    public class Student {
        @Id
        @Column(name = "sid")
        @GeneratedValue(generator = "uuid")
        private String id;
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        @JoinTable(name = "stu_cou", joinColumns = {@JoinColumn(name = "sid", nullable = false)}, inverseJoinColumns = { @JoinColumn(name = "cid", nullable = true) })
        private List<Courses> courses;
    }

You could use a NamedQuery in your Courses Entity with a join.

See here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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