简体   繁体   English

如何使用Hibernate HQL或Criteria内部连接两个表?

[英]How to inner join two tables using Hibernate HQL or Criteria?

@Entity
public class doctor {
   @Id
   private int id;
   private String username;
   private String password;
   private String phone;
   private String email;

   @OneToMany(targetEntity = patient.class, cascade = CascadeType.ALL, mappedBy = "doctor")
   @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
   private Collection<patient> patients = new ArrayList<patient>();
}

@Entity
public class patient {
   @Id
   private int id;
   private String name;
   private String surname;
   private String phone;
   private int systolic;
   private int diastolic;

   @ManyToOne
   private doctor doctor;
}

For now i can retreive only doctors information by this criteria: 目前我只能通过以下标准检索医生信息:

Criteria query = session.createCriteria(doctor.class);
query.createCriteria("patients", "p");
query.add(Restrictions.eq("p.phone", phone));
List<doctor> doctorList = (ArrayList<doctor>) query.list();

How i can with hibernate criteria retreive by giving patient phone, his doctor information and some patients information? 如何通过提供患者电话,医生信息和一些患者信息来恢复休眠标准?

Something like : phone=3423423424 , then answear is : 类似于:phone = 3423423424,然后是answear:

-------------doctor's info----------------------------------patientinfo(systolic,diastolic)----------------------- -------------医生的信息---------------------------------- patientinfo (收缩压,舒张压)-----------------------

  1 "Dr dre" sdfssd 243242 drdre@gmail.com  160 170

where 160 170 are the patient's information 其中160 170是患者的信息

If not with criteria, with HQL? 如果没有标准,使用HQL?

What you want is the following. 你想要的是以下内容。

With Hibernate Criteria API: 使用Hibernate Criteria API:

Criteria query = sessionFactory.getCurrentSession().
createCriteria(Patient.class, "patient");
query.setProjection(Projections.projectionList().
add(Projections.property("patient.doctor")).
add(Projections.property("patient.systolic")).
add(Projections.property("patient.diastolic")));
query.add(Restrictions.eq("patient.phone", phone));
return query.list();

With HQL (actually just JPQL): 使用HQL(实际上只是JPQL):

select p.doctor, p.systolic, p.diastolic from Patient p where p.phone = ?1

What you get in the result is value of Type List<Object[]> . 您在结果中得到的是Type List<Object[]> Also add @Cascade(value=CascadeType.SAVE_UPDATE) to doctor field on your Patient class. 同时将@Cascade(value=CascadeType.SAVE_UPDATE)添加到Patient类的doctor字段中。

您的查询仅返回医生信息(而不是患者的信息)的原因是因为对于OneToMany关系,默认情况下FetchType设置为LAZY ,如果指定获取类型为EAGER,则hibernate也将返回患者。

@OneToMany(targetEntity = patient.class, cascade = CascadeType.ALL, mappedBy = "doctor", fetch = FetchType.EAGER)

You have bidirectional mapping so from each doctor you can get his patients and from each patient you can get his doctor information. 您有双向映射,因此每位医生都可以得到患者,每位患者都可以获得医生信息。 If you need a list with patients instead a list of doctors just create analogous Criteria for patients. 如果您需要患者列表而不是医生列表,只需为患者创建类似的标准。 session.createCriteria(patient.class) , with needed restrictions. session.createCriteria(patient.class) ,具有所需的限制。 you don't have to make it eager. 你不必急于求成。 In most cases we don't need eager fetching. 在大多数情况下,我们不需要急切的提取。 It is much better initialize (Hibernate.initialize) the collection or proxy if you would need the objects outside the hibernate session. 如果你需要hibernate会话之外的对象,那么初始化(Hibernate.initialize)集合或代理要好得多。

btw use camel case when you name java classes. btw在命名java类时使用camel case。 It's widely used convention. 它是广泛使用的惯例。

In case you are using HibernateTemplate 如果您使用的是HibernateTemplate

String hql = "from Boo where id in (:listParam)";
String[] params = { "listParam" };
Object[] values = { list};
List boos = getHibernateTemplate().findByNamedParam(hql, params, values);

Quoted from Spring Forum 引自春季论坛

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM