5

我的影片由Spring和功能NHibernate(NHIB 3.1)無法解析屬性:User.Full_Name的:Harrods.Core.Entities.Teacher

供電這個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來驗證。但由於某些原因,一旦數據被加載,它會彈出錯誤和事務回滾。現在看來,這不再僅僅是查詢?

回答

4

你必須使用一個JoinQueryOverJoinAlias來完成你的要求的。

What is the difference between JoinQueryOver and JoinAlias?

就拿這篇文章中看看部分AssociationsAliaseshttp://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx

+0

這是我試過的,但stil我不工作,如果我寫錯了,請糾正我。:) return session.QueryOver () .JoinQueryOver (u => u.User).Where(x => x.Full_Name.IsInsensitiveLike(「 %」 + FullNameSearchFilter + 「%」)) .OrderBy(X => x.Id).ASC .Skip((跳到 - 1)*取) 。取(取) .List (); – RicL

+0

這兩種方法都無效! :/ JoinQueryOver和JoinAlias都給出相同的確切錯誤。 – RicL

+1

'Full_Name'是'User'中的映射列嗎?還有什麼是'MasterLicensee'?難道你不應該返回'老師'而不是'MasterLicensee'列表嗎? –

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)] 
+0

sql代碼正在正確顯示;我可以通過我的NHibernate Profiler來驗證。但由於某些原因,一旦數據被加載,它會彈出錯誤和事務回滾。現在看來,這不再僅僅是查詢? – RicL

+0

您的解決方案也可以運作!謝謝! – RicL