简体   繁体   中英

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 :

-------------doctor's info----------------------------------patientinfo(systolic,diastolic)-----------------------

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

where 160 170 are the patient's information

If not with criteria, with HQL?

What you want is the following.

With 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):

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[]> . Also add @Cascade(value=CascadeType.SAVE_UPDATE) to doctor field on your Patient class.

您的查询仅返回医生信息(而不是患者的信息)的原因是因为对于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. 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.

btw use camel case when you name java classes. It's widely used convention.

In case you are using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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