【问题标题】:how to write this query using hql如何使用 hql 编写此查询
【发布时间】:2018-04-06 01:12:19
【问题描述】:

这是我的查询:

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 编写查询。希望您的帮助。

【问题讨论】:

    标签: mysql hibernate hql


    【解决方案1】:

    试试这个语法:

    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'
    

    【讨论】:

    • 当我尝试这个查询时,在服务器输出中生成了一个查询。
    • ...输出是什么?
    • 评论太长了。我已将其作为帖子提交。
    【解决方案2】:

    @蒂姆·比格莱森-----> 这是输出

    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'    
    

    它给出了医生的所有结果,例如全名,密码,地址...... 但我想从医生表中获取 fullName 和 doctorId 以及从时间表表中获取其他数据。

    【讨论】:

    • Hibernate 返回对象,而不是记录。不过,我认为您可以指定要选择的字段,q.v。我更新的答案。
    【解决方案3】:

    试试这个

    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;  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-15
      • 2016-01-17
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2014-03-18
      相关资源
      最近更新 更多