HQL中的分组依据

时间:2016-12-18 16:54:26

标签: java spring hibernate hql

如何在hibernate中用HQL编写给定的sql并获取包含两列的对象数组

select specialization, count(specialization) 
from ocs_tbl_doctor group by specialization;

映射Bean类是

DoctorBean.java

@Entity
@Table(name = "OCS_TBL_DOCTOR")
public class DoctorBean {

@Id
@Column(name = "DOCTORID")
private String doctorID;

@Column(name = "DOCTORNAME")
private String doctorName;

@Column(name = "DATEOFBIRTH")
private Date dateOfBirth;

@Column(name = "DATEOFJOINING")
private Date dateOfJoining;

@Column(name = "GENDER")
private String gender;

@Column(name = "QUALIFICATION")
private String qualification;

@Column(name = "SPECIALIZATION")
private String specialization;

@Column(name = "YEARSOFEXPERIENCE")
private int yearsOfExperience;

@Column(name = "STREET")
private String street;

@Column(name = "LOCATION")
private String location;

@Column(name = "CITY")
private String city;

@Column(name = "STATE")
private String state;

@Column(name = "PINCODE")
private String pincode;

@Column(name = "CONTACTNUMBER")
private String contactNumber;

@Column(name = "EMAILID")
private String emailID;

//getters and setters
}

我在投射第二列时遇到问题,即(计数(专业化))

2 个答案:

答案 0 :(得分:0)

查看documentation

select doc.specialization, count(doc)
from DoctorBean doc
group by doc.specialization

答案 1 :(得分:0)

试试这个:

select d.specialization, count(d.specialization) 
from DoctorBean d  group by d.specialization;