tags:

views:

696

answers:

1

I'm having some trouble understanding a bit of code. I've got 2 classes Company and CompanyLocation. Mapped as:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.DataTransfer.CompanyLocation,Core.DataTransfer" table="`CompanyLocation`" lazy="true">
    <id name="CompanyLocationId" column="`CompanyLocationID`" type="int">
      <generator class="native" />
    </id>
    <many-to-one name="Company" cascade="none" column="`CompanyID`" not-null="true" />
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.DataTransfer.Company,Core.DataTransfer" table="`Company`" lazy="true">
    <id name="CompanyId" column="`CompanyID`" type="int">
      <generator class="native" />
    </id>
    <property type="string" not-null="true" length="100" name="CompanyName" column="`CompanyName`" />
    <bag name="CompanyLocations" inverse="true" lazy="true" cascade="all">
      <key column="`CompanyID`" />
      <one-to-many class="Core.DataTransfer.CompanyLocation,Core.DataTransfer" />
    </bag>
  </class>
</hibernate-mapping>

I'm trying to query using a Criteria query as follows:

        public IList<CompanyLocation> GetCompanyLocations(string sortExpression)
        {
            ICriteria criteria = _session.CreateCriteria(typeof(CompanyLocation));

            if (!string.IsNullOrEmpty(sortExpression))
            {
                Sort sort = new Sort(sortExpression);
                if (!string.IsNullOrEmpty(sort.AssociationPath))
                {
                    criteria.CreateCriteria(sort.AssociationPath).AddOrder(new Order(sort.SortColumn, sort.IsAscending));
                }
//                if (!string.IsNullOrEmpty(sort.AssociationPath))
//                {
//                    criteria.CreateCriteria(sort.AssociationPath);
//                }
//                criteria.AddOrder(new Order(sort.SortColumn, sort.IsAscending));
            }
            return criteria.List<CompanyLocation>();
        }

When I call the method and pass in a sortExpression of "Company.CompanyName DESC" the Sort class will parse it so that: AssociationPath = "Company" SortColumn = "CompanyName" IsAscending = false

The if block in the method that is not commented out works. If I change to the one that is commented out it fails with: could not resolve property: CompanyName

I'm having trouble understanding why.

+2  A: 

Most likely because CreateCriteria() creates a new criteria (instead of modifying original one as your code expects)

Try changing to

if (!string.IsNullOrEmpty(sort.AssociationPath))
{
   var criteriaCopy = criteria.CreateCriteria(sort.AssociationPath);
}
criteriaCopy.AddOrder(new Order(sort.SortColumn, sort.IsAscending));

or just plain

if (!string.IsNullOrEmpty(sort.AssociationPath))
{
   criteria = criteria.CreateCriteria(sort.AssociationPath);
}
criteria.AddOrder(new Order(sort.SortColumn, sort.IsAscending));
Alexander Uslontsev