2

I have an abstract base class that contains a property called "DateCreated"; it has a number of child classes, some of which link to each other.

When I try to query against one of the child classes, I get an ambiguous column error with DateCreated, as it is contained in more than one table.

Here is a snippet of the criteria:

.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty(Item.LinkField + ".Id"), "Measure")
.Add(Projections.Count("Id"), "StatCount")
.Add(Projections.GroupProperty("lnk." + Item.LinkTextField), "MeasureText")
.Add(Projections.SqlGroupProjection("MONTH(DateCreated) As [period]", "MONTH(DateCreated)", new[] { "period" }, new IType[] { NHibernateUtil.Int32 })))

Is there any way for me to specify as part of the criteria an alias for the join between a subclass and its parent? I don't think CreateAlias will work as there are no joining properties.

  1. If I prefix {alias}. to DateCreated, it will prefix _this in the generated SQL and while that is the alias of the current table, it is not the alias of the parent table which contains the DateCreated field, so the column is not found.

  2. If I prefix this_1_. to DateCreated which is the alias NHibernate gives to the parent table in the generated SQL, the query works, but I think that is probably a bad idea.

Here is the generated SQL from NHibernate:

SELECT TOP (5) this_.incidenttype_id      AS y0_,
               Count(this_.module_id)     AS y1_,
               lnk1_.dicttext             AS y2_,
               Month(this_1_.datecreated) AS [StatFrequency]
FROM   tblincident this_
       INNER JOIN tblmodule this_1_
               ON this_.module_id = this_1_.id
       INNER JOIN tbldict lnk1_
               ON this_.incidenttype_id = lnk1_.id
       LEFT OUTER JOIN tbldictcode lnk1_1_
                    ON lnk1_.id = lnk1_1_.dict_id
       LEFT OUTER JOIN tbldictimage lnk1_2_
                    ON lnk1_.id = lnk1_2_.dict_id
       LEFT OUTER JOIN tbldictsdscategory lnk1_3_
                    ON lnk1_.id = lnk1_3_.dictimage_id
WHERE  this_1_.customer_id = 2
       AND this_1_.isarchive = 0
       AND this_1_.isactive = 1
       AND this_1_.isconfidential = 0
       AND this_.incidentdate BETWEEN '2012/01/01' AND '2013/04/01'
       AND this_1_.isactive = 1
       AND this_1_.customer_id = 2
       AND this_1_.orgunit_id BETWEEN 3 AND 1000
GROUP  BY this_.incidenttype_id,
          lnk1_.dicttext,
          Month(this_1_.datecreated) 

In this example, tblIncident is the subclass of tblModule DateCreated is contained within tblModule The only reason the above criteria worked is because I passed in the "this_1_" as that is whats automatically assigned to it by nhibernate. using "{alias}" would output "this_1" which would cause an error.

I obviously want a way to specify that alias.

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
user1838662
  • 503
  • 7
  • 17

0 Answers0