无法解析属性:User.Full_Name:Harrods.Core.Entities.Teacher

时间:2011-11-05 16:29:12

标签: asp.net-mvc-3 spring nhibernate fluent-nhibernate queryover

我有这个由Spring和Fluent NHibernate(NHIB 3.1)驱动的ASP.NET MVC3代码

我有这个错误:

could not resolve property: User.Full_Name of: Harrods.Core.Entities.Teacher

[QueryException: could not resolve property: User.Full_Name of: Harrods.Core.Entities.Teacher]
NHibernate.Persister.Entity.AbstractPropertyMapping.GetColumns(String propertyName) +104
NHibernate.Persister.Entity.AbstractPropertyMapping.ToColumns(String alias, String propertyName) +57
NHibernate.Persister.Entity.BasicEntityPropertyMapping.ToColumns(String alias, String propertyName) +100
NHibernate.Persister.Entity.AbstractEntityPersister.ToColumns(String alias, String propertyName) +53
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumns(ICriteria subcriteria, String propertyName) +184
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetColumnsUsingProjection(ICriteria subcriteria, String propertyName) +134
NHibernate.Criterion.CriterionUtil.GetColumnNamesUsingPropertyName(ICriteriaQuery criteriaQuery, ICriteria criteria, String propertyName) +45
NHibernate.Criterion.CriterionUtil.GetColumnNames(String propertyName, IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria, IDictionary`2 enabledFilters) +46
NHibernate.Criterion.InsensitiveLikeExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters) +101
NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary`2 enabledFilters) +298
NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, ICriteria criteria, String rootEntityName, IDictionary`2 enabledFilters) +447
NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters) +175
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +412
NHibernate.Impl.CriteriaImpl.List(IList results) +80
NHibernate.Impl.CriteriaImpl.List() +104
NHibernate.Criterion.QueryOver`1.List() +96
NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver<TRoot>.List() +75

这是问题似乎来自的代码:

return session.QueryOver<Teacher>()
                    .Where(x => x.User.Full_Name.IsInsensitiveLike("%" + Search + "%"))
                    .OrderBy(x => x.Id).Asc
                    .Skip((skip - 1) * take)
                    .Take(take)
                    .List<Teacher>();

我的老师课程如下:

[Serializable]
public class Teacher
{

    public virtual User User { get; set; }
}

我的User类看起来像这样:

public class User : BaseEntity<User>
{

    public virtual string Email { get; set; }
    public virtual string Full_Name { get; set; }
}

不确定是什么问题。一切看起来都没问题。任何人都有任何想法错了吗? :)

不确定,但看起来有些相似,但不知道如何解决:

NHibernate.QueryException with dynamic-component A correct way to load entities by Id list when Id is not mapped

修改 试过这个,但仍然给出了同样的错误: -

return session.QueryOver<Teacher>()
                    .JoinQueryOver<User>(u => u.User)
                    .Where(x => x.Full_Name.IsInsensitiveLike("%" + FullNameSearchFilter + "%"))
                    .OrderBy(x => x.Id).Asc
                    .Skip((skip - 1) * take)
                    .Take(take)
                    .List<Teacher>();

尝试了别名..:

return session.QueryOver<Teacher>()
                    .JoinAlias(() => ML.User, () => U)
                    .Where(() => U.Full_Name.IsInsensitiveLike("%" + FullNameSearchFilter + "%"))
                    .OrderBy(x => x.Id).Asc
                    .Skip((skip - 1) * take)
                    .Take(take)
                    .List<Teacher>();

既不工作!

编辑2

正确显示sql代码;我可以通过我的NHibernate Profiler验证这一点。但由于某种原因,一旦加载了数据,它就会弹出错误并回滚事务。现在在我看来,这不仅仅是关于查询?

2 个答案:

答案 0 :(得分:4)

您必须使用JoinQueryOverJoinAlias来完成您的要求。

What is the difference between JoinQueryOver and JoinAlias?

在本文中查看AssociationsAliases部分:http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

答案 1 :(得分:2)

这对我有用:

User userAlias = null;
var list = session.QueryOver<Teacher>()
                  .JoinQueryOver(x => x.User, () => userAlias, JoinType.LeftOuterJoin)
                  .Where(x => x.FullName.IsInsensitiveLike("%" + "test" + "%"))
                  .OrderBy(x => x.Id).Asc
                  .Skip(1)
                  .Take(2)
                  .List(); //Without MasterLicensee

生成SQL(我使用SQL-CE进行测试):

SELECT
    this_.Id as Id2_1_,
    this_.User_id as User2_2_1_,
    useralias1_.Id as Id3_0_,
    useralias1_.Email as Email3_0_,
    useralias1_.FullName as FullName3_0_
FROM
    "Teacher" this_
left outer join
    "User" useralias1_
        on this_.User_id=useralias1_.Id
WHERE
    lower(useralias1_.FullName) like @p0
ORDER BY
    useralias1_.Id asc;
@p0 = '%test%' [Type: String (0)]