如何使用hql编写此查询

时间:2017-10-25 04:34:09

标签: mysql hibernate 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编写我的查询。希望你的帮助。

3 个答案:

答案 0 :(得分:0)

试试这个语法:

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'

答案 1 :(得分:0)

试试这个

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;  

答案 2 :(得分:0)

@Tim Biegeleisen -------&gt;  这是输出

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="owl-stage">
  <div class="owl-item active">
    <div class="item">
      <div class="slider-image"></div>
    </div>
  </div>
  <div class="owl-item">
    <div class="item">
      <div class="slider-image"></div>
    </div>
  </div>
  <div class="owl-item">
    <div class="item">
      <div class="slider-image"></div>
    </div>
  </div>
  <div class="owl-item">
    <div class="item">
      <video src="dummy" controls=""></video>
    </div>
  </div>
</div>
<a href="">Play Video</a>

它给出了医生的全部结果,如全名,密码,地址...... 但是我希望从医生表和时间表表中的其他数据中获取fullName和doctorId。