C# 如何使用Nhibernate查询行李
我需要获得国家列表中不包含国家的出版物(IsoCode2研究) sql查询是:C# 如何使用Nhibernate查询行李,c#,nhibernate,many-to-many,subquery,C#,Nhibernate,Many To Many,Subquery,我需要获得国家列表中不包含国家的出版物(IsoCode2研究) sql查询是: select * from pub_head ph where not exists (select 1 from pub_head_forbidden_country phfc , country c where phfc.pub_head_id = ph.pub_head_id and phfc.country_id = c.country_id
select * from pub_head ph
where not exists
(select 1 from pub_head_forbidden_country phfc , country c
where phfc.pub_head_id = ph.pub_head_id
and phfc.country_id = c.country_id
and c.iso_code2 = 'CA');
模型:
<class name="Publication" table="PUB_HEAD">
<id name="Id" column="PUB_HEAD_ID">
<generator class="native">
<param name="sequence">SEQ_PUB_HEAD</param>
</generator>
</id>
<idbag name="Countries" table="PUB_HEAD_COUNTRY" lazy="true">
<collection-id column="PUB_HEAD_COUNTRY_ID">
<generator class="native">
<param name="sequence">SEQ_PUB_HEAD_COUNTRY</param>
</generator>
</collection-id>
<key column ="PUB_HEAD_ID" />
<many-to-many class="Model.Referential.Country, Model" column="COUNTRY_ID"/>
</idbag>
</class>
<class name="Country" table="Country">
<id name="Id" column="COUNTRY_ID">
<generator class="native">
</generator>
</id>
<property name="Name">
<column name="NAME"></column>
</property>
<property name="IsoCode2">
<column name="ISO_CODE2"></column>
</property>
<property name="IsoCode3">
<column name="ISO_CODE3"></column>
</property>
</class>
序号:酒馆馆长
SEQ_PUB_HEAD_COUNTRY
我从子查询开始,但没有成功
谢谢对加入过敏吗?这种SQL很难理解 是这个意思吗
SELECT *
FROM pub_head
WHERE id not IN (
SELECT phfc.pub_head_id
FROM pub_head_forbidden_country as phfc
INNER JOIN country AS c ON phfc.country_id = c.country_id
WHERE c.iso_code2 = 'CA'
)
[作为答案发布,因为此SQL在评论中会很糟糕。]SQL也可能是这样的
SELECT * FROM pub_head ph
WHERE ph.pub_head_id not IN (
SELECT phfc.pub_head_id
FROM pub_head_forbidden_country phfc
INNER JOIN country c ON phfc.country_id = c.country_id
WHERE c.iso_code2 = 'CA'
)
您想在hql中还是使用criteria api进行查询?在您发布的sql查询中,pub_head_probled_country表是什么?pub_head_probled_country表类似于pub_head_country表(很抱歉复制/粘贴)。我没有在您的模型中看到pub_head_probled_country映射。如果未映射,则无法使用NHibernate查询它。您是说您有一个从示例中忽略的Publication.BankedenCountries集合吗?是的,我对联接过敏;)这样的查询不是语义更丰富,也更容易混淆吗?或者也许我的大脑太弱太懒了[顺便说一句:我想试着回答,但我不知道如何解决你的问题。]