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.  [ 7 posts ] 
Author Message
 Post subject: Cannot define alias by HQL
PostPosted: Wed Aug 22, 2007 5:41 am 
Newbie

Joined: Wed Aug 22, 2007 5:18 am
Posts: 4

Hi,

I have problem in defining alias in HQL.
I want to give aliases to the objects of the projection in HQL.
The HQL and the funnction call are like this -->
session.CreateQuery("select d.Product.Name as ProductName, d.Remarks as Remarks from OrderDetail as d")
.SetResultTransformer(Transformers.AliasToBean(typeof(ValueProduct)))
.List();

I get an exception while calling the above code, and the message of the exception is this
{", expected in SELECT [select d.Product.Name as ProductName, d.Remarks as Remarks from Sandbox.Domain.OrderDetail as d]"}

By the way, I can obtain an array of object[] with product name and remarks by calling this HQL,
session.CreateQuery("select d.Product.Name, d.Remarks from OrderDetail as d").List();

I think I can't define the alias in the HQL. How can I fix this ?

My aim is to list the projection objects by using ResultTransformer. I can do this by CreateSQLQuery.SetResultTransformer and CreateCriteria.SetResultTransformer successfully.

The createCriteria version is like this

ProjectionList prjList = Projections.ProjectionList();
prjList.Add(Projections.Alias(Projections.Property("P.Name"), "ProductName"));
prjList.Add(Projections.Alias(Projections.Property("Remarks"), "Remarks"));

return session.CreateCriteria(typeof(OrderDetail))
.CreateAlias("Product", "P")

.SetProjection(prjList)

.SetResultTransformer(Transformers.AliasToBean(typeof(ValueProduct)))
.List();

I cannot use the HQL with the result transformer as a result of the above.

Thanks alot


Hibernate version:1.2.0.GA

Mapping documents:
<class name="OrderDetail" table="TBL_OrderDetail">
<id name="Id" column="Id" type="Int64">
<generator class="identity"/>
</id>

<property name="Head" column="OrderHeadId" type="Int64"/>
<many-to-one name ="Product" column="ProductID" class="Product"/>
<property name="Remarks" type="String"/>

</class>

<class name="Product" table="TBL_Product">
<id name="Id" column="Id" type="Int64">
<generator class="identity"/>
</id>

<property name="Name" type="String"/>
<property name="Description" type="String"/>
<property name="Barcode" type="String"/>

</class>


Name and version of the database you are using: SQL Server 2005


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 22, 2007 6:46 am 
Newbie

Joined: Tue Sep 26, 2006 10:59 am
Posts: 15
This is example how to create query with join.


StringBuilder queryRD = new StringBuilder();

queryRD.Append("select c.ID, rd.Name ");
queryRD.Append("from Cottage as c ");
queryRD.Append("left outer join c.RailwayDirection as rd");

IList lstRD = session.CreateQuery(queryRD.ToString()).List();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 22, 2007 10:19 am 
Newbie

Joined: Wed Aug 22, 2007 5:18 am
Posts: 4
Thanks for your reply
My problem is not using join.
I cannot not define alias for the projection in HQL, as a result I cannot use
result transformer for it.

I want to do something like this in HQL
say: "select d.Name as DepartmentName, d.Head as HeadMaster from Department as d"
and then I can use Transformers.AliasToBean(typeof(DepartmentContainer))
which can help me to store DepartmentName and HeadMaster as a object and list the result a DepartmentContainer[].

However, I can't define alias for d.Name and d.Head. It simply throw an exception with ", expected" in the message. Do you have any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 22, 2007 10:21 am 
Newbie

Joined: Wed Aug 22, 2007 5:18 am
Posts: 4
Thanks for your reply
My problem is not using join.
I cannot not define alias for the projection in HQL, as a result I cannot use
result transformer for it.

I want to do something like this in HQL
say: "select d.Name as DepartmentName, d.Head as HeadMaster from Department as d"
and then I can use Transformers.AliasToBean(typeof(DepartmentContainer))
which can help me to store DepartmentName and HeadMaster as a object and list the result as DepartmentContainer[].

However, I can't define alias for d.Name and d.Head. It simply throw an exception with ", expected" in the message. Do you have any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 6:51 am 
Newbie

Joined: Wed Aug 22, 2007 5:18 am
Posts: 4
I can found example using alias in the select clause

select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat

http://www.hibernate.org/hib_docs/refer ... ryhql.html

Also, I have found the issue EJB-294
http://opensource.atlassian.com/project ... se/EJB-294
which stated what I have encountered.

QueryException is thrown while using "as" in the select clause.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 05, 2007 4:55 am 
Newbie

Joined: Wed Sep 05, 2007 4:38 am
Posts: 1
Location: Emsdetten, Germany
I got the same problem using aliases, but just removing the (optional) "as" clause does not help.

_________________
-hangy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2008 10:03 am 
Newbie

Joined: Tue Jan 16, 2007 4:44 am
Posts: 5
Hello,

I have exactly the same problem, it looks like using the "as" in the SELECT clause of a HQL query cause the ", expected in SELECT" exception, no matter you use a join or not.

If somebody knows a workaround, I would be glad to be informed.

Thanks in advance

Manitra.

_________________
=====
Nhibernate rox !

here is my (empty) blog


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

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.