Java 在同一个表上使用多个联接进行休眠
我有两个postgreSQL表preference和date_etl,preference_date_etl存储它们的映射 首选项\u日期\u etl的休眠映射:Java 在同一个表上使用多个联接进行休眠,java,sql,hibernate,postgresql,hql,Java,Sql,Hibernate,Postgresql,Hql,我有两个postgreSQL表preference和date_etl,preference_date_etl存储它们的映射 首选项\u日期\u etl的休眠映射: <hibernate-mapping> <class name="com..bla.bla.PreferenceDateETL" table="preference_date_etl"> <id name="id" column="id" unsaved-valu
<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>
问题:在order by子句中创建的preference2\日期\不在选择列表中,因此异常select DISTINCT,order by表达式必须出现在选择列表中
问:为什么hibernate在同一个表上使用两个连接INNER和CROSS。如果有按顺序列表创建的首选项1。日期,那么一切都会很好。想法?您可以重新构造此查询,以避免使用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
需要明确的是,偏好和日期之间存在多对多关系?不是一对一吗?
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
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