NHibernate QueryOver Multiple Order By with Aliases
Asked Answered
S

2

5

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.

Stagecraft answered 3/11, 2011 at 17:24 Comment(0)
S
1

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.

Stagecraft answered 11/11, 2011 at 9:47 Comment(1)
Why use the old method ? Is not there any solution for QueryOver ?Heteronomous
D
9

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.

Dangerous answered 9/10, 2012 at 12:43 Comment(2)
Yup, OrderBy should be used NOT OrderByAliasConfidante
THIS. Why the hell is the method's name "OrderByAlias" if it does not order it by alias?Hatbox
S
1

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.

Stagecraft answered 11/11, 2011 at 9:47 Comment(1)
Why use the old method ? Is not there any solution for QueryOver ?Heteronomous

© 2022 - 2024 — McMap. All rights reserved.