2010-12-20 52 views
0

一個袋子,我需要得到該國名單中不包含一個國家(上IsoCode2研究)出版物如何查詢與NHibernate

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 
       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> 

我從SubQueries開始,但我沒有成功做到這一點。

謝謝

+0

你想要做的HQL查詢或與標準api?什麼是你發佈的SQL查詢中的pub_head_forbidden_​​country表? – Max 2010-12-20 17:41:20

+0

使用條件API – Martyrian 2010-12-20 18:01:00

+0

pub_head_forbidden_​​country表格與pub_head_country表格相似(對於複製/粘貼) – Martyrian 2010-12-20 18:01:40

回答

0

過敏加入?這個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將是一個評論太可怕了。]

+0

是的,我對連接過敏;) – Martyrian 2010-12-20 18:03:06

+0

是不是查詢更多的語義,更像這樣混淆?或者,也許我的大腦只是虛弱而懶散。 :) [順便說一句:我會嘗試回答,但我不知道如何解決您的問題。] – ANeves 2010-12-20 19:30:32

0

的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' 
)