2016-11-17 58 views
1

我需要檢索所有用戶有效的Wish屬性(因此不爲空)。這是我班的XML:Nhibernate與空的子查詢

<class name="Project.Engine.Domain.User,Project.Engine" table="Users" lazy="true"> 
    <id name="UserID" column="UserID"> 
    <generator class="native" /> 
    </id> 
    <property name="Firstname" column="Firstname" type="string" not-null="true" 
    length="255" /> 
    <property name="Lastname" column="Lastname" type="string" not-null="true" 
    length="255" /> 
    <property name="Email" column="Email" type="string" not-null="true" 
    length="255" /> 
    <one-to-one name="Wish" cascade="all" property-ref="UserID" 
    class="Project.Engine.Domain.Wish, Project.Engine" /> 
</class> 

讓我的所有用戶的方法如下:

public PagedList<User> GetAll(int pageIndex, int pageSize, 
    string orderBy, string orderByAscOrDesc) 
{ 
    using (ISession session = NHibernateHelper.OpenSession()) 
    { 
     var users = session.CreateCriteria(typeof(User)); 
     users.Add(Restrictions.IsNotNull("Wish")); 
     return users.PagedList<User>(session, pageIndex, pageSize); 
    } 
} 

正如你可以看到,我已經添加的子對象的限制。這種方法無法正常工作,因爲該方法會將包含Wish屬性的所有用戶返回爲空。任何幫助?

這是對孩子的xml:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    <class name="Project.Engine.Domain.Wish,Project.Engine" table="Wish" lazy="false"> 
    <id name="WishID" column="WishID"> 
     <generator class="native" /> 
    </id> 
    <property name="UserID" column="UserID" type="int" not-null="true" length="32" /> 
    <property name="ContentText" column="ContentText" type="string" not-null="false" length="500" /> 
    <property name="Views" column="Views" type="int" not-null="true" length="32" /> 
    <property name="DateEntry" column="DateEntry" type="datetime" not-null="true" /> 
    </class> 
</hibernate-mapping> 
+0

你也應該包含'Wish'映射。 –

+0

我已更新該帖子 – Ras

回答

1

那麼,有一個錯誤one-to-onenull測試可能不存在的一面。我已經遇到它但忘了它。 property-ref只是使診斷有點棘手,但它確實存在於實際one-to-one

這是NHibernate跟蹤工具中相應的issue

解決方法:測試nullWish的不可空值屬性的狀態,如Wish.Views

原諒胡亂猜測的測試語法,我不使用了,因爲多年,但例如嘗試:

public PagedList<User> GetAll(int pageIndex, int pageSize, 
    string orderBy, string orderByAscOrDesc) 
{ 
    using (ISession session = NHibernateHelper.OpenSession()) 
    { 
     var users = session.CreateCriteria(typeof(User)); 
     users.Add(Restrictions.IsNotNull("Wish.Views")); 
     return users.PagedList<User>(session, pageIndex, pageSize); 
    } 
} 

使用,我確認這變通辦法用我自己的項目,通過實例給出了:

// The "TotalAmount != null" seems to never be able to come false from a 
// .Net run-time view, but converted to SQL, yes it can, if TransactionRecord 
// does not exist. 
// Beware, we may try "o.TransactionsRecord != null", but you would get struck 
// by https://nhibernate.jira.com/browse/NH-3117 bug. 
return q.Where(o => o.TransactionsRecord.TotalAmount != null); 

我保持我的其他的答案,因爲你可能會考慮使用many-to-one代替,特別是因爲你沒有做了bidirectionnal映射(沒有相應constrainedone-to-one in Wish)以及沒有實際的one-to-onemany-to-one不會受到該錯誤的影響。

0

one-to-one使用映射property-ref是不是一個「實際的」一比一,而通常這是一個標誌一個many-to-one映射應改爲使用。
也許這與你的麻煩無關,但你可以試一試。

「實際」一對一的從屬表主鍵等於父表主鍵。 (相關表格,Wish你的情況,會在你的情況下,國外主鍵,UserId見此example)。

我有一段「打」與「一到一個屬性-REF」,和由於許多問題,我總是放棄它。我用更多的經典映射來替換它,或者改變我的db以實現一對一,或者使用多對一併且在子節點上與集合一起生活,儘管它總是包含單個元素。