2011-03-20 59 views
3

我有兩個多到一個引用(標題&說明)到名爲TEXTREF單表產品表:多個多到一個單個表

產品:

<many-to-one lazy="false" name="Title" class="TextRef" column="TitleRef" fetch="join" cascade="save-update"/> 
<many-to-one lazy="false" name="Description" class="TextRef" column="DescriptionRef" fetch="join" cascade="save-update"/> 

每TEXTREF有一個一對多到TextRefItem表:

<bag lazy="false" name="Values" table="TextRefItem" cascade="save-update" inverse="true" fetch="join"> 
    <key column="TextId"></key> 
    <one-to-many class="TextRefItem"/> 
</bag> 

現在我想加載所有TextRefItem(S)爲標題一氣呵成&說明,但僅NHibernate的CRE吃了第一基準(標題)

SELECT this_.ProductId as ProductId7_2_, this_.CategoryId as CategoryId7_2_, 
this_.DescriptionRef as Descript3_7_2_, 
textref2_.TextId as TextId8_0_, values3_.TextId as TextId4_, 
values3_.TextItemId as TextItemId4_, values3_.TextItemId as TextItemId9_1_,values3_.LangId as LangId9_1_, 
values3_.Text as Text9_1_, values3_.TextId as TextId9_1_ 
FROM 
Product this_ 
inner join TextRef textref2_ on this_.DescriptionRef=textref2_.TextId 
left outer join TextRefItem values3_ on textref2_.TextId=values3_.TextId 
WHERE this_.ProductId = 1 

的另一個(說明)它使一個單獨的選擇查詢

我怎麼能告訴NHibernate的,以避免加入?

回答

1

我不知道爲什麼一個單獨的查詢是爲第二個引用(說明)。但它有可能延長通過指標分析抓取策略加載所有TextRefs和TextRefItems產品一個炮打響:

var criteria = session.CreateCriteria(typeof(Product)) 
    .SetFetchMode("Description.Values", FetchMode.Join); 

criteria.SetResultTransformer(new DistinctRootEntityResultTransformer()); 
criteria.Add(Restrictions.Eq("ProdId", 1)); 
var list = criteria.List<Product>(); 
+0

謝謝,我想使這個默認的行爲,但似乎這是不可能通過使用映射文件 – sos00 2011-03-20 22:58:23