Question

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;
    }
Was it helpful?

Solution

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

See here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top