简体   繁体   中英

NHibernate Projection With Null Many-To-One

criteria.CreateAlias("ChildObject", "c");
IProjection fullProjection = Projections.ProjectionList()
           .Add(Projections.Property("Item"), "Item")
           .Add(Projections.Property("c.SubItem"), "ChildObject.SubItem")

This Object has properties Item and ChildObject .

ChildObject has a property SubItem .

NHibernate can use this projection to successfully query and list all Objects with ChildObject.

However, ChildObject can be null on some rows. This projection seems to just skip them entirely. They never make it to my Transformer. I think NHibernate makes these projections as required to not be null in the criteria.

So I though I could outsmart it by doing:

Projections.Conditional(Restrictions.Eq("ChildObject", null), nullProjection, fullProection)

Where nullProjection doesn't have ChildObject projected at all, but then it just complains ....

Both true and false projections must return the same types.

Is there anyway to do this projection and get nullable values? Or am I going to have to do two separate queries?

This had to do with left joins instead of inner joins.

criteria.CreateAlias("ChildObject", "c");

Would create SQL statements as inner joins, but using....

criteria.CreateAlias("ChildObject", "c", LeftJoin);

enforces left joins and they handle when the value doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM