HQL query doesnt accept column aliases??

3,620 views
Skip to first unread message

Fethi Gürcan

unread,
Mar 30, 2009, 9:36:38 AM3/30/09
to nhusers
Hello,

i'm newbie in nhibernate and i've 2 simple question.

1) i'm trying some simple HQL queries for learning querying in hibernate.

                            "select c.Id, c.PlakaNo, c.Name from Il c"        (selected columns are less than the table's original, so it returns IList<object[]>)

    this is a simple query that i run.

But when i add some aliases like this: "select c.Id as xx, c.PlakaNo as yy, c.Name as zz from Il c"

it throws this exception : " ',' expected in SELECT before:as [select c.Id as xx, c.PlakaNo as yy, c.Name as zz from Il c] "

i see many samples that uses column aliases.

what's the point that i missed?

and after that.

2) It seems i have to use IResultTransformer for "custom" query results (for search results, lists etc.) for trnasforming well defined generic list<DTO>.
(and it seems Transformers.AliasToBean() needs aliases for work right?)

Note: i can't run the AliasToEntity() cause question (1). But i'd write my own custom IResultTransformer class successfully. But i think it is not the good choice in many cases.
(this approach needs Trasformer for each individual DTOs)

First: is it the main approach for results like this example?
Second: or what is the main approach that are you using?

Thanks









--
Fethi Gürcan

Tyler Burd

unread,
Mar 30, 2009, 7:45:48 PM3/30/09
to nhu...@googlegroups.com

Hi there!

 

In my understanding, HQL does NOT accept aliases in the select clause.  It does accept aliases in the from and any join clauses, though.  You may have seen samples like this:

 

“select motherCat.Id, c.Id from Cat c join c.Mom as motherCat

 

As you can see, the aliases are in the from and join clauses.  This allows you to easily reference them in the select clause.

 

There is really no point in adding aliases in the select clause as you attempted to do, because NH does not allow you to reference the returned results by alias, just by the position in the IList<object[]>.

 

Does that help?

 

-tyler burd

 


Kenneth Siewers Møller

unread,
Mar 31, 2009, 4:21:48 AM3/31/09
to nhu...@googlegroups.com
If you need to select a new object you can use the "select new Cat(c.x, c.y, c.z) from Cat c where ... ". Aliases is not useful when doing a simple select.
Otherwise you can use the Criteria API and do a projection instead.

Unfortunately it seems like HQL and Criteria API supports different features. But perhaps that's the point.

2009/3/30 Fethi Gürcan <fethi...@gmail.com>

Fethi Gürcan

unread,
Apr 1, 2009, 5:12:54 AM4/1/09
to nhu...@googlegroups.com
Hello thanks for the answers.

firstly, i can't realize why i need to select a new object? Actually what different between;

"select c from Cat c where .." and "select new Cat(c.x,c.y,c.z) from Cat c where ..."

Is it like below?:

- Might i write/use a alternative constructor of Cat with the x,y,z parameters? (i assume Cats have more porperties than x,y,z)

- Can i write any other (not mapped) simple class (e.g. CatHuman) with some properties. And "select new CatHuman(c.x,c.y,c.z) from Cat c where ..." and this query returns IList<CatHuman> right?.

- Actually i'm planning to use Criterias for most cases. can i "select new" in criterias?

- it seems i can use "select new" instead of IResultTransformer. right?

- "select new" really looks cool. any limitations? when i have to use IResultTransformer instead of "select new"?

Thanks
Fethi






2009/3/31 Kenneth Siewers Møller <kenneth...@gmail.com>



--
Fethi Gürcan

Fethi Gürcan

unread,
Apr 1, 2009, 7:37:59 AM4/1/09
to nhu...@googlegroups.com
Hello again. I found answers to most of my questions. Others are still waits answers :)

See below;

01 Nisan 2009 Çarşamba 12:12 tarihinde Fethi Gürcan <fethi...@gmail.com> yazdı:

Hello thanks for the answers.

firstly, i can't realize why i need to select a new object? Actually what different between;

"select c from Cat c where .." and "select new Cat(c.x,c.y,c.z) from Cat c where ..."

Is it like below?:

- Might i write/use a alternative constructor of Cat with the x,y,z parameters? (i assume Cats have more porperties than x,y,z)
(YES)


- Can i write any other (not mapped) simple class (e.g. CatHuman) with some properties. And "select new CatHuman(c.x,c.y,c.z) from Cat c where ..." and this query returns IList<CatHuman> right?.
(YES, we have to import this class with [Import] attibute or <import class=""/> tag in mapping file)


- Actually i'm planning to use Criterias for most cases. can i "select new" in criterias?
(??)


- it seems i can use "select new" instead of IResultTransformer. right?
(YES)

- "select new" really looks cool. any limitations? when i have to use IResultTransformer instead of "select new"?
(??)



--
Fethi Gürcan
Reply all
Reply to author
Forward
0 new messages