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.  [ 9 posts ] 
Author Message
 Post subject: Query and Alias
PostPosted: Thu Nov 22, 2007 7:38 am 
When I try to assign an alias to a column in named query, I get a compilation query error.

Example:

<query name="PersonTest1">
<![CDATA[

SELECT
p.FirstName as Name,
p.LastName as Name2
FROM Person p

]]>
</query>

I want to execute this query an transform to an DTO (with Transformers.AliasToBean) but I get error for the alias.


Top
  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
HQL is not the same as SQL, although they look very similar. Just use "from Person" and get a list of Person objects back and you have all the properties on the Persons; that's the power of NHibernate. If you just want the first name and last name, drop the column aliases and you will get a list of Object arrays. "result[0][0]" and "result[0][1]" gives you the first name and last name from the first row respectively.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 9:39 am 
Ok,

But, how can I do to bind this result to a GridView?


Top
  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 10:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
mbogliacino wrote:
how can I do to bind this result to a GridView?


You can bind the IList<Person> to a GridView just like how you would with a DataSet or DataTable.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 10:55 am 
Yes,

But, I dont want bind the IList<Person>. I just want bind IList with Name and LastName.


Top
  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 10:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
This has just become an ASP.NET question... you don't have to show all the properties in the GridView. You can set up the GridView to have only two columns to show the Name and LastName property. If you don't know how to do that, I suggest you post the question in an ASP.NET forum.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 11:18 am 
I know how to do that,

But If I do IList<Person> NHibernate load all collumns from the table, and if I do "select Name, LastName from Person" NHibernate load only this columns.


Top
  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 11:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
This HQL will load only the Name and LastName into an array of object[] (i.e. object[][]):
Code:
select p.Name, p.LastName from Person p

This HQL will load the properties into a LightPerson class:
Code:
select new LightPerson(p.Name, p.LastName) from Person p
... provided LightPerson has a matching constructor.
See here:
http://www.hibernate.org/hib_docs/nhibe ... hql-select

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 11:56 am 
ok,

Thanks


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.