我想要做的是能够使用带有查询的投影。我已经使用AliasToBean Transformer在第一级使用投影成功完成了这项操作,但是当我从详细类中投射属性时,nhibenate会引发以下异常:
Could not find a setter for property 'FirstContact' in class 'Model.Personnel.Entities.Employee'
at NHibernate.Properties.ChainedPropertyAccessor.GetSetter(Type theClass, String propertyName)
at NHibernate.Transform.AliasToBeanResultTransformer.TransformTuple(Object[] tuple, String[] aliases)
at NHibernate.Loader.Criteria.CriteriaLoader.GetResultList(IList results, IResultTransformer customResultTransformer)
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.List[T]()
at NHibernate.Criterion.QueryOver`1.List[U]()
at NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver<TRoot>.List[U]()
at Tests.Entities.EmployeeFacts.QueryEmployees()
这是我目前的代码:
Employee anEmployee = null;
Contact aContact = null;
session.QueryOver(() => anEmployee).Left
.JoinAlias(() => anEmployee.Contact, () => aContact)
.OrderBy(() => aGender.Name).Asc
.ThenBy(() => aContact.FirstContact).Asc
.SelectList(builder => builder.Select(() => aContact.FirstContact)
.WithAlias(() => aContact.FirstContact)
.Select(() => anEmployee.FirstName)
.WithAlias(() => anEmployee.FirstName))
.TransformUsing(Transformers.AliasToBean(typeof(Employee)))
.List<Employee>();
答案 0 :(得分:0)
对于您的选择列表,所有WithAlias
方法应该使用相同的dto,并且也应该与变换器中使用的类相同。另外,我不知道你为什么投射到映射的实体?预测是针对平坦的。
您看到的错误是由于无效.WithAlias(() => aContact.FirstContact)
... NH转换器现在需要Employee类中的FirstContact属性。解决方案是创建一个包含FirstContact和FirstName属性的dto,并将其用于WithAlias和AliasToBean转换器。