具有多个表的标准Eager join

时间:2012-12-06 13:18:36

标签: hibernate hibernate-criteria detachedcriteria

代码

        DetachedCriteria criteria2 = DetachedCriteria.forClass(MasterResult.class);
        criteria2.createAlias("masterCCHolders", "masterCCHolders", CriteriaSpecification.INNER_JOIN);
        criteria2.createAlias("masterFlights", "masterFlights", CriteriaSpecification.INNER_JOIN);
        criteria2.createAlias("masterPassengers", "masterPassengers", CriteriaSpecification.INNER_JOIN);
        criteria2.createAlias("masterVendors", "masterVendors", CriteriaSpecification.INNER_JOIN);

这是生成以下查询

select
    this_.id as id49_4_,
    this_.adults as adults49_4_,
    this_.bookingdate as bookingd3_49_4_,
    this_.children as children49_4_,
    this_.depart_date as depart5_49_4_,
    this_.email1 as email6_49_4_,
    this_.fareprice as fareprice49_4_,
    this_.first_name as first8_49_4_,
    this_.infants as infants49_4_,
    this_.last_name as last10_49_4_,
    this_.phonenumber as phonenu11_49_4_,
    this_.selectiontime as selecti12_49_4_,
    this_.mastercurrentstatus as masterc13_49_4_,
    masterccho2_.master_booking_id as master4_49_6_,
    masterccho2_.id as id6_,
    masterccho2_.id as id47_0_,
    masterccho2_.first_name as first2_47_0_,
    masterccho2_.last_name as last3_47_0_,
    masterccho2_.master_booking_id as master4_47_0_,
    masterflig3_.master_booking_id as master5_49_7_,
    masterflig3_.id as id7_,
    masterflig3_.id as id50_1_,
    masterflig3_.direction as direction50_1_,
    masterflig3_.fromcode as fromcode50_1_,
    masterflig3_.master_booking_id as master5_50_1_,
    masterflig3_.tocode as tocode50_1_,
    masterpass4_.master_booking_id as master6_49_8_,
    masterpass4_.id as id8_,
    masterpass4_.id as id46_2_,
    masterpass4_.first_name as first2_46_2_,
    masterpass4_.last_name as last3_46_2_,
    masterpass4_.master_booking_id as master6_46_2_,
    masterpass4_.middle_name as middle4_46_2_,
    masterpass4_.type as type46_2_,
    mastervend5_.master_booking_id as master4_49_9_,
    mastervend5_.id as id9_,
    mastervend5_.id as id48_3_,
    mastervend5_.locator as locator48_3_,
    mastervend5_.master_booking_id as master4_48_3_,
    mastervend5_.name as name48_3_ 
from
    masterresult this_ 
left outer join
    masterccholder masterccho2_ 
        on this_.id=masterccho2_.master_booking_id 
left outer join
    masterflight masterflig3_ 
        on this_.id=masterflig3_.master_booking_id 
left outer join
    masterpassenger masterpass4_ 
        on this_.id=masterpass4_.master_booking_id 
left outer join
    mastervendor mastervend5_ 
        on this_.id=mastervend5_.master_booking_id 

查询返回20行,这很好,但是这也创建了2个masterResult对象。

模型结构就像

MasterResult包含

  • 一套masterCCHolders,
  • 一套masterFlights,
  • 一组masterPassengers,
  • 一组masterVendors

如何解决,这个问题?

1 MasterResult包含

  • 2套masterCCHolders,
  • 2套masterFlights,
  • 5套masterPassengers,
  • 1套masterVendors,

2 * 2 * 5 = 20条记录

我如何拥有一个masterResult对象,其中包含一组大小为2的masterCCholder,一组大小为2的masterflights,一组masterpassengers大小为5等等。

由于

1 个答案:

答案 0 :(得分:1)

criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);