NHibernate HQL: left outer join with “with” clause does not work

六眼飞鱼酱① 提交于 2019-11-30 03:57:30

问题


In an EAV system, I have a mapping that looks like this:

<class name="Record">
   <map name="Values" table="RecordFieldValue">
      <key column="RecordFK">
      <index column="FieldFK">
      <element column="Value">
   </map>
</class>

I would like to select some Records, ordered by the value of each Record for a specific Field. However, note that not all Records will actually have a Value for that Field. In this case, the record should still be fetched and sorted with a null value.

The desired SQL would look like this:

select rec.*, val.Value
from Record rec
left outer join RecordFieldValue val
on val.RecordFK = rec.PK and val.FieldFK = :field
order by val.Value

After a lot of digging, I found that the correct way to modify the "on" clause of the left join in HQL is with the "with" keyword (see https://nhibernate.jira.com/browse/NH-514). So I tried this HQL:

from Record rec
left join rec.Values vals with index(vals) = :field
order by vals

Unfortunately, this produces the following error: with-clause expressions did not reference from-clause element to which the with-clause was associated. So I tried this instead:

from Record rec
left join rec.Values vals with index(rec.Values) = :field
order by vals

But that produced a new error: with clause can only reference columns in the driving table.

Any ideas on how to get this work? Thanks.

-- Brian


回答1:


This works:

from Record rec
left join rec.Values vals with vals.index = :field
order by vals

Not exactly intuitive or well-documented, but it gets the job done.



来源:https://stackoverflow.com/questions/4467722/nhibernate-hql-left-outer-join-with-with-clause-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!