我正在努力创建一个Hibernate条件查询以获取所有数据,如下所示我需要一个房间中所有约会的列表,但每个房间只有一个约会。
public static List<Chamber> getChambersInfoOfDoctor(Long id) {
print("Finding chambers of doctor: " + id);
List<Chamber> chambers = new ArrayList<Chamber>();
Session session = null;
try
{
session = HibernateUtils.getSessionFactory().openSession();
session.beginTransaction();
Criteria c = session
.createCriteria(Chamber.class).add(Restrictions.eq("doctor.id", id));
c.setFetchMode("appointments", FetchMode.DEFAULT)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
chambers = c.list();
for(Chamber chamber: chambers) {
Hibernate.initialize(chamber.getAppointments());
}
session.getTransaction().commit();
}
catch (RuntimeException re) {
if(session != null) session.getTransaction().rollback();
re.printStackTrace();
print("finding patient: " + re);
} finally {
return chambers;
}
}
以下是Appointment.java
public class Appointment extends BaseBO {
...
private Doctor doctor;
private Patient patient;
private Chamber chamber;
private WorkingDay workingDay;
private Slot slot;
...
HBM
<hibernate-mapping package="com.hibernate.relationships.doctor">
<class name="Appointment" table="appointment" catalog="consult_doctor">
...
<many-to-one name="doctor" class="Doctor" fetch="select">
<column name="doctor_id" not-null="true" />
</many-to-one>
<many-to-one name="patient" class="Patient" fetch="select">
<column name="patient_id" not-null="true" />
</many-to-one>
<many-to-one name="chamber" class="Chamber" fetch="select">
<column name="chamber_id" not-null="true" />
</many-to-one>
<many-to-one name="slot" class="Slot" fetch="select">
<column name="slot_id" not-null="true" />
</many-to-one>
<many-to-one name="workingDay" class="WorkingDay" fetch="select">
<column name="working_day_id" not-null="true" />
</many-to-one>
Chamber.java
public class Chamber extends BaseBO {
...
private Doctor doctor;
private Set<WorkingDay> workingDays = new HashSet<WorkingDay>(0);
private Set<Appointment> appointments = new HashSet<Appointment>(0);
...
HBM
<hibernate-mapping package="com.hibernate.relationships.doctor">
<class name="Chamber" table="chamber" catalog="consult_doctor">
<many-to-one name="doctor" class="Doctor" fetch="select">
<column name="doctor_id" not-null="true" />
</many-to-one>
<set name="appointments" table="appointment"
inverse="true" lazy="true" fetch="select">
<key>
<column name="id" not-null="true" />
</key>
<one-to-many class="Appointment" />
</set>
<set name="workingDays" table="chamber_day"
inverse="false" lazy="true" fetch="select" >
<key>
<column name="chamber_id" not-null="true" />
</key>
<many-to-many entity-name="com.hibernate.relationships.doctor.WorkingDay">
<column name="working_day_id" not-null="true" />
</many-to-many>
</set>
</class>
Doctor.java
public class Doctor extends BaseBO {
...
private Set<Chamber> chambers = new HashSet<Chamber>(0);
private Set<Appointment> appointments = new HashSet<Appointment>(0);
...
HBM
<hibernate-mapping package="com.hibernate.relationships.doctor">
<class name="Doctor" table="doctor" catalog="consult_doctor">
<set name="chambers" table="chamber"
inverse="true" lazy="false" fetch="select">
<key>
<column name="doctor_id" not-null="true" />
</key>
<one-to-many class="Chamber" />
</set>
<set name="appointments" table="appointment"
inverse="true" lazy="true" fetch="select">
<key>
<column name="doctor_id" not-null="true" />
</key>
<one-to-many class="Appointment" />
</set>
</class>
Patient.java
public class Patient extends BaseBO {
private Set<Appointment> appointments = new HashSet<Appointment>(0);
...
HBM
<hibernate-mapping package="com.hibernate.relationships.doctor">
<class name="Patient" table="patient" catalog="consult_doctor">
...
<set name="appointments" table="appointment"
inverse="true" lazy="true" fetch="select">
<key>
<column name="patient_id" not-null="true" />
</key>
<one-to-many class="Appointment" />
</set>
</class>
Slot.java
public class Slot extends BaseBO {
private Set<WorkingDay> workingDays = new HashSet<WorkingDay>(0);
private Set<Appointment> appointments = new HashSet<Appointment>(0);
HBM
<hibernate-mapping package="com.hibernate.relationships.doctor">
<class name="Slot" table="slot" catalog="consult_doctor">
<set name="workingDays" table="day_slot"
inverse="false" lazy="true" fetch="select" cascade="none" >
<key>
<column name="slot_id" not-null="true" />
</key>
<many-to-many entity-name="com.hibernate.relationships.doctor.WorkingDay">
<column name="working_day_id" not-null="true" />
</many-to-many>
</set>
<set name="appointments" table="appointment"
inverse="true" lazy="true" fetch="select">
<key>
<column name="slot_id" not-null="true" />
</key>
<one-to-many class="Appointment" />
</set>
</class>
WorkingDay.java
public class WorkingDay extends BaseBO {
private Set<Chamber> chambers = new HashSet<Chamber>(0);
private Set<Slot> slots = new HashSet<Slot>(0);
private Set<Appointment> appointments = new HashSet<Appointment>(0);
...
HBM
<hibernate-mapping package="com.hibernate.relationships.doctor">
<class name="WorkingDay" table="working_day" catalog="consult_doctor">
<set name="chambers" table="chamber_day" cascade="none"
inverse="false" lazy="true" fetch="select" >
<key>
<column name="working_day_id" not-null="true" />
</key>
<many-to-many entity-name="com.hibernate.relationships.doctor.Chamber">
<column name="chamber_id" not-null="true" />
</many-to-many>
</set>
<set name="slots" table="day_slot"
inverse="false" lazy="true" fetch="select" cascade="none" >
<key>
<column name="working_day_id" not-null="true" />
</key>
<many-to-many entity-name="com.hibernate.relationships.doctor.Slot">
<column name="slot_id" not-null="true" />
</many-to-many>
</set>
<set name="appointments" table="appointment"
inverse="true" lazy="true" fetch="select">
<key>
<column name="working_day_id" not-null="true" />
</key>
<one-to-many class="Appointment" />
</set>
</class>