0

我是 Hibernate 标准的新手,Tutor和之间的关系StudentOneToMany(即一个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

以下是我拥有的学生和导师实体:

学生.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

}

导师.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 和标准我没有得到相同的结果吗?

谢谢。

4

1 回答 1

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)
于 2013-03-24T12:50:31.987 回答