NHibernate实体通过投影访问 [英] NHibernate Entity access through projection

查看:91
本文介绍了NHibernate实体通过投影访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此映射文件:

'class name="WebTools.Data.Common.IHObjekt, WebTools.Data" table="IHObjekt"'  
  ....
'property name="TYPBEZEICH" type="string"' 
...  
'many-to-one name="standort" column="STANDORT_ID" fetch="join"'  

我想使用 Projections.ProjectionList()以减少查询返回的列数。

And I would like to use a 'Projections.ProjectionList()' to reduce the number of returned columns from the query.

我这样做:

'ICriteria criteria = Session.CreateCriteria(typeof(Data.Common.IHObjekt));'  
'ProjectionList projectionList = Projections.ProjectionList();'  
..   
'projectionList.Add(Projections.Property("standort.CODE"));'  

并收到此错误:

NHibernate.QueryException:无法解析属性:standort.CODE of:WebTools.Data.Common.IHObjekt

NHibernate.QueryException: could not resolve property: standort.CODE of: WebTools.Data.Common.IHObjekt

我正在尝试访问子实体,但看来我只能访问自己的值家长班。像这样:

I am trying to access a child entity but it appears I can only access the values in my parent class. Like:

'projectionList.Add(Projections.Property("TYPBEZEICH"));' 

有人可以提供一些代码让我使用投影来访问子类中的实体吗?

Can anyone provide some code that will let me use projection to access an entity in a child class?

推荐答案

您需要创建一个别名才能引用子实体的属性。

You need to create an alias in order to reference properties of the child entity.

ICriteria criteria = Session.CreateCriteria(typeof(Data.Common.IHObjekt));

criteria.CreateAlias("standort", "s", JoinType.InnerJoin);

ProjectionList projectionList = Projections.ProjectionList();
projectionList.Add(Projections.Property("s.CODE"));

这篇关于NHibernate实体通过投影访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆