简洁版本
Hibernate 正在执行多个选择查询而不是仅两个查询来检索一对一的关系
长版
人物类:
@Entity
@Table(name = "people")
public class Person{
@Id
@GeneratedValue
@Column
private Integer id;
@Column
private String name;
@OneToOne(optional=false, fetch=FetchType.EAGER)
private Job job;
}
工作类别:
@Entity
@Table(name = "jobs")
public class Job{
@Id
@GeneratedValue
@Column
private Integer id;
@Column
private String name;
}
显示所有人员及其工作:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Person> people = session.createQuery("from Person").list();
for(Person p : people){
System.out.println(p.getName() + " | " + p.getJob().getName());
}
我所期望的:两个选择,五个人一个,三个不同工作的一个(三个人有相同的工作)
我得到了什么:四个选择,一个用于人员,三个用于每个不同的工作
Hibernate: select person0_.id as id1_, person0_.job_id as job3_1_, person0_.name as name1_ from people person0_
Hibernate: select job0_.id as id0_0_, job0_.name as name0_0_ from jobs job0_ where job0_.id=?
Hibernate: select job0_.id as id0_0_, job0_.name as name0_0_ from jobs job0_ where job0_.id=?
Hibernate: select job0_.id as id0_0_, job0_.name as name0_0_ from jobs job0_ where job0_.id=?
Jim | Programmer
Andrew | DBA
Tomas | Sysadmin
Henry | Sysadmin
Jerry | Sysadmin
如果我有 1000 个人从事 1000 个不同的工作,我会看到 1000 个选择被执行。我怎样才能让 Hibernate 只做两个选择?
谢谢