如何使用 hql 编写此查询
how to write this query using hql
这是我的查询:
SELECT d.fullName,d.doctorId,d.speciality, t.hospital, t.date, t.time
FROM Doctor d, TimeTable t
WHERE d.doctorId = t.doctorId and d.fullName = 'Subash Nisam' and t.date = '2017.03.02'
ORDER BY t.date;
我有两张桌子->Doctor 和 TimeTable
@Entity
public class TimeTable {
private int timeTableId;
private String time;
private String date;
private String hospital;
private Doctor doctor;
@Id
@GeneratedValue(strategy = AUTO)
public int getTimeTableId() {
return timeTableId;
}
public void setTimeTableId(int timeTableId) {
this.timeTableId = timeTableId;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHospital() {
return hospital;
}
public void setHospital(String hospital) {
this.hospital = hospital;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "doctorId", nullable = false)
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
}
//////////////////////////////////////////
@Entity
public class Doctor {
private int doctorId;
private String fullName;
private String regNo;
private String designation;
private String speciality;
private String address;
private String contactNo;
private String email;
private String workingTime;
private String password;
private String branch;
---------------------------------------------
@Id
@GeneratedValue(strategy = AUTO)
public int getDoctorId() {
return doctorId;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "doctor")
public Set<TimeTable> getTimeTables() {
return timeTables;
}
public void setTimeTables(Set<TimeTable> timeTables) {
this.timeTables = timeTables;
}
}
我想使用 hql 编写查询。希望你的帮助。
试试这个语法:
select d.fullName, d.doctorId, d.speciality, t.hospital, t.date, t.time
from Doctor as d
inner join d.timeTables t
where d.fullName = 'Subash Nisam' and t.date = '2017-03-02'
试试这个
select Doctor.fullname, Doctor.doctorId, Doctor.speciality, TimeTable.hospital, TimeTable.date, TimeTable.time from Doctor inner join TimeTable on Doctor.doctorId =TimeTable.doctorId where Doctor.fullname='Subash' and Timetable.date='2017-03-02' order by Timetable.date;
@Tim Biegeleisen------>
这是输出
select
doctor0_.doctorId as doctorId1_3_0_,
timetables1_.timeTableId as timeTabl1_5_1_,
doctor0_.address as address2_3_0_,
doctor0_.branch as branch3_3_0_,
doctor0_.contactNo as contactN4_3_0_,
doctor0_.designation as designat5_3_0_,
doctor0_.email as email6_3_0_,
doctor0_.fullName as fullName7_3_0_,
doctor0_.password as password8_3_0_,
doctor0_.regNo as regNo9_3_0_,
doctor0_.speciality as special10_3_0_,
doctor0_.workingTime as working11_3_0_,
timetables1_.date as date2_5_1_,
timetables1_.doctorId as doctorId5_5_1_,
timetables1_.hospital as hospital3_5_1_,
timetables1_.time as time4_5_1_
from
Doctor doctor0_
inner join
TimeTable timetables1_
on doctor0_.doctorId=timetables1_.doctorId
where
doctor0_.fullName='Subash Nisam'
and timetables1_.date='2017.03.02'
它给出了医生的所有结果,例如全名、密码、地址...
但我想从 doctor table 获取 fullName 和 doctorId,并从 timetable table.
获取其他数据
这是我的查询:
SELECT d.fullName,d.doctorId,d.speciality, t.hospital, t.date, t.time
FROM Doctor d, TimeTable t
WHERE d.doctorId = t.doctorId and d.fullName = 'Subash Nisam' and t.date = '2017.03.02'
ORDER BY t.date;
我有两张桌子->Doctor 和 TimeTable
@Entity
public class TimeTable {
private int timeTableId;
private String time;
private String date;
private String hospital;
private Doctor doctor;
@Id
@GeneratedValue(strategy = AUTO)
public int getTimeTableId() {
return timeTableId;
}
public void setTimeTableId(int timeTableId) {
this.timeTableId = timeTableId;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHospital() {
return hospital;
}
public void setHospital(String hospital) {
this.hospital = hospital;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "doctorId", nullable = false)
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
}
//////////////////////////////////////////
@Entity
public class Doctor {
private int doctorId;
private String fullName;
private String regNo;
private String designation;
private String speciality;
private String address;
private String contactNo;
private String email;
private String workingTime;
private String password;
private String branch;
---------------------------------------------
@Id
@GeneratedValue(strategy = AUTO)
public int getDoctorId() {
return doctorId;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "doctor")
public Set<TimeTable> getTimeTables() {
return timeTables;
}
public void setTimeTables(Set<TimeTable> timeTables) {
this.timeTables = timeTables;
}
}
我想使用 hql 编写查询。希望你的帮助。
试试这个语法:
select d.fullName, d.doctorId, d.speciality, t.hospital, t.date, t.time
from Doctor as d
inner join d.timeTables t
where d.fullName = 'Subash Nisam' and t.date = '2017-03-02'
试试这个
select Doctor.fullname, Doctor.doctorId, Doctor.speciality, TimeTable.hospital, TimeTable.date, TimeTable.time from Doctor inner join TimeTable on Doctor.doctorId =TimeTable.doctorId where Doctor.fullname='Subash' and Timetable.date='2017-03-02' order by Timetable.date;
@Tim Biegeleisen------> 这是输出
select
doctor0_.doctorId as doctorId1_3_0_,
timetables1_.timeTableId as timeTabl1_5_1_,
doctor0_.address as address2_3_0_,
doctor0_.branch as branch3_3_0_,
doctor0_.contactNo as contactN4_3_0_,
doctor0_.designation as designat5_3_0_,
doctor0_.email as email6_3_0_,
doctor0_.fullName as fullName7_3_0_,
doctor0_.password as password8_3_0_,
doctor0_.regNo as regNo9_3_0_,
doctor0_.speciality as special10_3_0_,
doctor0_.workingTime as working11_3_0_,
timetables1_.date as date2_5_1_,
timetables1_.doctorId as doctorId5_5_1_,
timetables1_.hospital as hospital3_5_1_,
timetables1_.time as time4_5_1_
from
Doctor doctor0_
inner join
TimeTable timetables1_
on doctor0_.doctorId=timetables1_.doctorId
where
doctor0_.fullName='Subash Nisam'
and timetables1_.date='2017.03.02'
它给出了医生的所有结果,例如全名、密码、地址... 但我想从 doctor table 获取 fullName 和 doctorId,并从 timetable table.
获取其他数据