如果我们有复合主键,如何在hibernate中使用ID获取数据集?

时间:2015-06-11 15:09:48

标签: mysql hibernate nhibernate-mapping hibernate-mapping

我有实体(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;
}
}
在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;
}

在上面我们从学生那里得到一套学生成绩单,一对多,多对一。

我们从 StudentEmpSkill

获取学生

在JoinColumn中,我们提供了 studentID 列来获取该项目。

现在我想从StudentEmpSkills获取StudentSkill对象。

StudentSkills - (studentID * | skillID *)| skillname

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

(..) - 复合主键

所以我想从StudentEmpSkill

获取StudentSkills

我需要在StudentEmpSkills中编写以获取StudentSkill的内容。因为我们在StudentSkill中有复合主键。

如何将StudentSkill映射到StudentEmpSkills。

有人可以建议吗?

0 个答案:

没有答案