我有两个 postgreSQL 表 preference and 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
in order by 子句不在选择列表中,因此异常 SELECT DISTINCT,ORDER BY 表达式必须出现在选择列表中
。
问题:为什么 hibernate 在同一个表上使用两个连接 INNER 和 CROSS。如果 ORDER BY 列表中有 preference1_.date_created
那么一切都会很好。想法?
最佳答案
您可以重构此查询以避免使用 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
关于java - Hibernate 在同一张表上使用多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25545569/