Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate QueryOver Multiple Order By with Aliases

This is my scenario:

 public IEnumerable<ISuperMerc> getSuperMercTree(string IDLanguage)
 {

      SuperMercModel SMer = null;
      SuperMercDescriptionModel descrSMer = null;
      MercModel Merc = null;
      MercDescriptionModel descrMerc = null;

      var qOver = _HibSession.QueryOver<SuperMercModel>(() => SMer)                    
                .JoinAlias(() => SMer.DescriptionsSM, () => descrSMer,JoinType.LeftOuterJoin)
                    .Where(() => descrSMer.IDLanguage == IDLanguage)
                .JoinAlias(() => SMer.Merc, () => Merc,JoinType.LeftOuterJoin)
                    .JoinAlias(() => Merc.DescriptionsMer, () => descrMerc,JoinType.LeftOuterJoin)
                        .Where(() => descrMerc.IDLanguage == IDLanguage)    
                .OrderByAlias(() => SMer.ID).Asc
                .ThenByAlias(() => descrMerc.Description).Asc 
                .Future<SuperMercModel>();

      return qOver;

 }

I've encountered the following error

could not resolve property: Description of: SuperMercModel

It's strange, the Description field is in the MercDescriptionModel class not in the SuperMercModel class.

I'm using aliases to create a multiple-join and multiple-order-by query.

like image 369
Faber Avatar asked Nov 03 '11 17:11

Faber


2 Answers

Your original code should work without using SQL if you change your OrderByAlias and ThenByAlias to regular OrderBy and ThenBy clauses. I had this same issue and that resolved it for me.

like image 110
stoneMaster Avatar answered Oct 28 '22 19:10

stoneMaster


I've solved it using HQL Here the code

public IEnumerable<ISuperMerc> getSuperMercTree(string IDLanguage)
{
    string hql = string.Empty;

     hql = "select SM from SuperMercModel SM"
                + " left join fetch SM.DescriptionsSM DSM"
                + " left join fetch SM.Merc ME"
                + " left join fetch ME.DescriptionsMer DME"
                + " where DSM.IDLanguage = :IDLanguage "
                + " and DME.IDLanguage = :IDLanguage "
                + " order by SM.ID asc, DME.Description asc";

            IQuery query = _HibSession
                .CreateQuery(hql)
                .SetString("IDLanguage ", IDLanguage );

            IList<SuperMercModel> result = query.List<SuperMercModel>();


            return result;
}

I hope it's helpful for someone.

like image 30
Faber Avatar answered Oct 28 '22 19:10

Faber