These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: SQL query: column alias injection problem
PostPosted: Fri Sep 07, 2007 12:43 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
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 ...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.