在项目采用Hibernate关联,采用关联使用比较简单,在关联时添加条件,减轻迪卡儿集的从而提高效率!
项目写HQL时使用的其中一个实例如下:
StringBuilder sb = new StringBuilder();
sb.append(" from Hotel h, RoomType r left join r.roomTypeSpecialPrices as rp with rp.from <= :checkInDate and rp.to >=:checkOutDate left join rp.roomTypeSpecialPriceInfos as ri with ri.dayNo=1,RoomTypePrice p ,RoomAmount a right join a.roomStatus as s, RoomTypePriceValidTime v\r\n");
// 基本外键关联条件
sb.append(" where h.id = r.hotelId and r.id = p.roomTypeId and r.id = s.roomTypeId and r.id = v.roomTypeId" +
" and s.statusDate between p.from and p.to\r\n");
备注:如果在不采用with方式,将with中条件添加到where之后相等于,将关联表信息查询交叉之后过滤。
采用with方式,相等于在查询交叉之前先过滤,减轻数据库负担。