我有两个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
问题:order by子句中的preference2_.date_created
不在选择列表中,因此异常SELECT DISTINCT, ORDER BY expressions must appear in select list
。
问题:为什么hibernate在同一个表上使用两个连接INNER和CROSS。如果ORDER BY列表中有preference1_.date_created
,那么一切都会好的。想法?
答案 0 :(得分:0)
您可以重新构建此查询以避免使用distinct关键字,从而对结果进行排序而不会出现问题。为此,请将PreferenceDateETL对象上的条件设置为子查询,然后检索链接到子查询中的PreferenceDateETL对象的首选项对象组中的所有Preference对象。这是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