Student 类与 Resume 类型的 Object 具有 OneToOne 关联,Resume 类与 Master 类型的集合具有 OneToMany 关联,而 master 具有属性 degreeName。我想要实现的是:
选择 student.resume.masters.degreeName 在 (?,?,?,.....) 中的所有学生
它应该搜索所有集合(主)对象。
下面的代码没有编译错误,但没有给出预期的结果,请纠正我。
学生实体:
@Entity
public class Student {
@OneToOne(fetch = FetchType.LAZY, mappedBy = "student", cascade = CascadeType.ALL)
private Resume resume;
}
简历实体:
@Entity
public class Resume {
@OneToMany(mappedBy="resume",cascade=CascadeType.ALL)
private List<Master> masters=new ArrayList<>();
@OneToOne(fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
private Student student;
}
主实体:
@Entity
public class Master {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="resume_id")
private Resume resume;
private String degreeName;
}
道:
Criteria studentCriteria = session.createCriteria(Student.class);
Criteria resumeCriteria = studentCriteria.createCriteria("resume");
Criteria mastersCriteria = resumeCriteria.createCriteria("masters");
List<String> degreeslist = new ArrayList<>(Arrays.asList(degrees));
//degreeList is collection of values on which the student will be searched
if (degreeslist.size() == 1) {
mastersCriteria.add(Restrictions.eq("degreeName",
degreeslist.get(0)));
} else {
mastersCriteria.add(Restrictions.in("degreeName", degreeslist));
}