从hibernate中的多个表中检索结果

时间:2015-11-21 13:55:13

标签: java mysql hibernate

我有三个表DriverReservation以及vehicle

表格定义

    Driver (table)                              Vehicle (table)
    ------------                                ---------------   
    DriverId   varchar (pk)                     VehicleId varchar  (pk)
    DriverName varchar                          VehicleName varchar  


    Reservation (table)
    -------------------                     (pk) --> Primary Key
    ReservationId varchar   (pk)            (fk) --> Foreign Key
    DriverId      varchar   (fk)
    VehicleId     varchar   (fk)
    JourneyDate   Date          

PoJo Classes

Driver

@Entity
@Component  
@Table(name="ATA_TBL_DRIVER")
public class DriverBean {

  @Id
  @Column(name="DRIVERID")
  private String driverID;

  @Column(name="NAME")
  private String name;

  @OneToMany(mappedBy = "driverBean")
  private Set<ReservationBean> reservationBeans;

  // getters and setters
}

VehicleBean

@Entity
@Component
@Table(name="ATA_TBL_VEHICLE")
public class VehicleBean {

   @Id
   @Column(name="VEHICLEID")
   private String vehicleID;

   @Column(name="NAME")
   private String name;

   @OneToMany(mappedBy="vehicleBean")
   private Set<ReservationBean> reservationBeans;

   //getters and setters
}

ReservationBean

@Entity
@Component
@Table(name="ATA_TBL_RESERVATION") 
public class ReservationBean {

   @Id
   @Column(name="RESERVATIONID")
   private String reservationID;    

   @Column(name="JOURNEYDATE")
   private Date journeyDate;

   @ManyToOne
   @JoinColumn(name="DRIVERID")
   private DriverBean driverBean;

   @ManyToOne
   @JoinColumn(name="VEHICLEID")
   private VehicleBean vehicleBean;

    //getters and setters
}
  

假设:   如果特定日期的预订表中存在driverid和vehicleid,则表示该日期已预订驾驶员,车辆

Question : I need to query the database for driverid and vehicleid which are not booked on particular journeydate ?

我对上述问题的解决方案是

驱动程序

Select driverid from ata_tbl_driver where driverid != (select driverid from ata_tbl_reservation where journeydate='21-JAN-15'。我在SQLPlus中也为vehicle做过类似的事情。我得到了预期的结果

但是当我在Hibernate中运行上面的查询时 session.createSQLQuery(Select driverid from ata_tbl_driver where driverid != (select driverid from ata_tbl_reservation where journeydate='21-JAN-15'),我没有得到任何结果。我不知道为什么?

有没有更好的方法来完成上述任务。如果是这样,请帮助。

0 个答案:

没有答案