2016-11-17 70 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以实现一对一,或者使用多对一并且在子节点上与集合一起生活,尽管它总是包含单个元素。