NHibernate Entity access through projection

这一生的挚爱 提交于 2019-12-11 05:48:44

问题


I have this mapping file:

'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"'  

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

I do this:

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

And receive this error:

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?


回答1:


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"));


来源:https://stackoverflow.com/questions/3921457/nhibernate-entity-access-through-projection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!