How to fetch set of data using ID in hibernate if we have composite primary key?

113 views Asked by At

I have Entities (Student,StudentSkills, StudentEmpSkills)

Student.java

 @Entity
 @Table(name = "Student", catalog = "dbs")
 public class Student implements java.io.Serializable {

 private int id;
 ...
 ...

 private Set<StudentSkills> studentSkills= new HashSet<StudentSkills>(0);
 private Set<StudentEmpSkills> studentEmpSkills= new HashSet<StudentEmpSkills>(0);


@OneToMany(fetch = FetchType.EAGER, mappedBy = "Student")
public Set<StudentSkills> getStudentSkills() {
    return this.studentEmpSkills;
}

public void setStudentSkills(
        Set<StudentSkills> studentSkills) {
    this.studentSkills = studentSkills;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "Student")
public Set<StudentEmpSkills> getStudentEmpSkills() {
    return this.StudentEmpSkills;
}

public void setStudentEmpSkills(
        Set<StudentEmpSkills> studentEmpSkills) {
    this.studentEmpSkills= studentEmpSkills;
}
}

in StudentEmpSkills.java

@Entity
@Table(name = "studentEmpSkills", catalog = "npdbs")
public class StudentEmpSkills implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private StudentEmpSkillsId id;
private Student Student ;

......


 @ManyToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "studentID", nullable = false, insertable = false, updatable = false)
 public Student getStudent() {
    return student;
}

In the above we are getting the Set of StudentEmpSkils from Student by one to many and many to one relation.

as we are getting Student from StudentEmpSkill

In JoinColumn we are giving studentID column to fetch the abject.

Now I want to get StudentSkill object from StudentEmpSkills.

StudentSkills - (studentID* | skillID*) | skillname

StudentEmpSkills - (studentID* | skillID* | empID*) | empName

(..) - composit primary key

So I want to fetch StudentSkills from StudentEmpSkill

What I need to write in StudentEmpSkills to fetch StudentSkill. because we have composit primary key in StudentSkill.

How to map the StudentSkill to StudentEmpSkills.

Can anyone please suggest?

0

There are 0 answers