Hibernate version: 1.2.0.GA
Section 13.1.4 of the NHibernate 1.2 Reference says:
Quote:
Column alias injection is needed in the following query (which most likely will fail):
Code:
sess.CreateSQLQuery("SELECT c.*, m.* FROM CATS c, CATS m WHERE c.MOTHER_ID = c.ID")
.AddEntity("cat", typeof(Cat))
.AddEntity("mother", typeof(Cat))
The intention for this query is to return two Cat instances per row, a cat and its mother. This will fail since there
is a conflict of names since they are mapped to the same column names and on some databases the returned
column aliases will most likely be on the form "c.ID", "c.NAME", etc. which are not equal to the columns specificed
in the mappings ("ID" and "NAME").
The following form is not vulnerable to column name duplication:
Code:
sess.CreateSQLQuery("SELECT {cat.*}, {mother.*} FROM CATS c, CATS m WHERE c.MOTHER_ID = c.ID")
.AddEntity("cat", typeof(Cat))
.AddEntity("mother", typeof(Cat))
This doesn't work. In order for me to get it to work, the entity aliases and the table aliases must match, because NHibernate expands
Code:
{alias.*}
to
Code:
alias.col1,
alias.col2,
...
Am I doing something wrong, or is the documentation wrong (again)? I assume the documentation is wrong, because it looks like this query wouldn't work anyway -- the WHERE clause should be
Code:
c.MOTHER_ID = m.ID
Doesn't anyone test the documentation examples before the documentation is published? This isn't the first time they don't work ...