我是Hibernate标准的新手,Tutor
和Student
之间的关系是OneToMany
(即一个Tutor
有很多Student
),我试过以下hibernate查询检索所有导师姓名为Peter Smith的学生:
from Student student where student.tutor.name is 'Peter Smith'
并获得以下结果:
Shin Don
Robert Cage
然后我尝试使用Hibernate Criteria API进行以下相同的查询:
Criteria criteria = session.createCriteria(Student.class);
criteria.createCriteria("tutor").add(Restrictions.eq("name", "Peter Smith"));
criteria.list();
这次我得到以下结果:
Shin Don
Shin Don
Robert Cage
Robert Cage
以下是我的学生和导师实体:
Student.java
@Entity
public class Student {
private Integer id;
private String studentId;
private String name;
private Tutor tutor;
//remaining fields
public Student() {}
public Student(String studentId, String name) {
super();
this.studentId = studentId;
this.name = name;
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
@Column(unique=true, nullable=false)
public String getStudentId() {
return studentId;
}
@ManyToOne
@JoinColumn(name="tutor_id")
public Tutor getTutor() {
return tutor;
}
@Override
public int hashCode() {
//returns hashcode based on studentId
}
@Override
public boolean equals(Object obj) {
//returns true or false based on studentId
}
@Override
public String toString() {
return this.name;
}
//remaining getter and setter methods
}
Tutor.java
@Entity
public class Tutor{
private Integer id;
private String tutorId;
private String name;
private Set<Student> students = new HashSet<Student>();
//remaining fields
public Tutor() {}
public Tutor(String tutorId, String name) {
super();
this.tutorId = tutorId;
this.name = name;
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
@Column(unique=true, nullable=false)
public String getTutorId() {
return tutorId;
}
@OneToMany(mappedBy="tutor", targetEntity=Student.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public Set<Student> getStudents() {
return students;
}
@Override
public int hashCode() {
//returns hashcode based on tutorId
}
@Override
public boolean equals(Object obj) {
//returns true or false based on tutorId
}
public void addStudent(Student student) {
this.students.add(student);
student.setTutor(this);
}
//remaining setter and getter methods
}
对于HQL,我得到以下SQL:
select
...
...
from
Tutor tutor0_
left outer join
Student students1_
on tutor0_.id=students1_.tutor_id
where
tutor0_.id=?
对于标准,我得到以下SQL:
select
...
...
from
Student this_
inner join
Tutor tutor1_
on this_.tutor_id=tutor1_.id
left outer join
Student students4_
on tutor1_.id=students4_.tutor_id
where
tutor1_.name=?
有人可以帮助我理解为什么我没有得到与给定的HQL和标准相同的结果?
感谢。
答案 0 :(得分:1)
更改Tutor.java
@OneToMany(mappedBy="tutor", targetEntity=Student.class,
cascade=CascadeType.ALL, fetch=FetchType.EAGER)
到
@OneToMany(mappedBy="tutor", targetEntity=Student.class,
cascade=CascadeType.ALL, fetch=FetchType.LAZY)