pankaj
pankaj

Reputation: 553

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

Upvotes: 1

Views: 702

Answers (1)

cbach
cbach

Reputation: 394

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

See here.

Upvotes: 3

Related Questions