2014-08-28 65 views
2

我有两个postgreSQL表首选项,date_etl和preference_date_etl存储它们的映射。休眠在同一个表上使用多个联接

preference_date_etl的Hibernate映射:

<hibernate-mapping> 

    <class name="com..bla.bla.PreferenceDateETL" 
     table="preference_date_etl"> 
     <id name="id" column="id" unsaved-value="null"> 
      <generator class="sequence"> 
       <param name="sequence"> 
      <![CDATA[preference_date_etl_id_seq]]> 
       </param> 
      </generator> 
     </id> 

     ...things.... 
    </class> 
</hibernate-mapping> 

所以,现在当我执行一个HQL喜欢:

select distinct pd.preference from PreferenceDateETL pd where pd.corporation.id=:corporationId and pd.preference.employee.deleted=false and pd.deleted=false and pd.preference.deleted=false and pd.dateETL.localDate>=:startDM and pd.dateETL.localDate<=:endDM and pd.preference.approvalStatus!=:approvalStatus order by pd.preference.dateCreated 

转换为SQL:

select 
     distinct preference1_.id as id1_76_, 
     ....things... 
    from 
     preference_date_etl preference0_ 
    inner join 
     preference preference1_ 
      on preference0_.preference_id=preference1_.id cross 
    join 
     preference preference2_ cross 
    join 
     employee employee3_ cross 
    join 
     date_etl dateetl5_ 
    where 
     ...things... 
    order by 
     preference2_.date_created 

问题:preference2_.date_created在ORDER BY子句不在选择列表中,因此例外SELECT DISTINCT, ORDER BY expressions must appear in select list

问题:为什么在同一个表上使用两个连接INNER AND CROSS来休眠。如果在ORDER BY列表中有preference1_.date_created那么一切都会很好。想法?

+0

只是要清楚 - 这是偏好和date_etl之间的多对多关系?不是一对一的? – Hedley 2014-08-28 17:08:22

回答

0

您可以重构此查询以避免使用distinct关键字,因此可以毫无问题地为您的结果排序。为此,请将PreferenceDateETL对象的条件设置为子查询,然后检索链接到子查询中PreferenceDateETL对象的首选项对象组中的所有首选对象。这里是HQL:

from Preference p where p in 
    (select pd.preference from PreferenceDateETL pd 
    where pd.corporation.id=:corporationId 
    and pd.deleted=false 
    and pd.dateETL.localDate>=:startDM 
    and pd.dateETL.localDate<=:endDM) 
and p.employee.deleted=false 
and p.deleted=false 
and p.approvalStatus != :approvalStatus 
order by p.dateCreated