-->
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.  [ 13 posts ] 
Author Message
 Post subject: Select properties query
PostPosted: Mon Apr 11, 2011 6:31 am 
Newbie

Joined: Mon Apr 11, 2011 4:19 am
Posts: 11
Hi,

I face an issue with my query with some of the properties been specified in the query.

Eg: My query looks like,
SELECT student.firstName, student.lastName, student.rollNo FROM Student AS student

I assume, this query returns a list of Student with values been set to the properties specified in the query and all other properties to null reference.

But the result was a list of Object array with the values. Is there something wrong in my assumption Or Hibernate does not support such an option ?

If no support available, I would b interested to know the reason behind no providing such support.

Thanks,
Senthilkumar Arumugam


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 6:58 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This is the expected and documented behavior. But Hibernate has support for several other forms. For example,

  • as a list using select new list(...).
  • a map using select new map(...).
  • as any other object using select new Foo(...) assuming that the class Foo has a matching constructor.

For more information and some examples see: http://docs.jboss.org/hibernate/core/3. ... hql-select


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 7:16 am 
Newbie

Joined: Mon Apr 11, 2011 4:19 am
Posts: 11
Hi,

Thanks for your quick response.

I understand, its documented behavior but I would like to know why it is not supported in Hibernate.

I build my query dynamically with the fields, hence I cannot go for constructor approach. I would like to get some idea, which helps me achieving to get a result of list of Students, with only minimal properties (at times I would need to get the associated object properties too).

Eg: SELECT student.lastName, student.rollNo, student.department.name FROM Student AS student

Thanks,
Senthilkumar Arumugam


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 7:45 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know why the Hibernate team choose that particular solution. You may have to look at creating your own ResultTransformer implementation to get the desired behaviour: http://docs.jboss.org/hibernate/core/3. ... ormer.html

My guess is that the already supported features are using one of the existing result transformers. What about the AliasToBeanResultTransformer implementation? Seems like it can use setter methods to partially initialize a bean class.


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 10:03 am 
Newbie

Joined: Mon Apr 11, 2011 4:19 am
Posts: 11
Hi,

Thank you again for your quick response. I do not think, it provides support to set the values for associated object property.

Am I doing something wrong ?

Thanks,
Senthilkumar Arumugam


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 12:41 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
One solution would be to move the 3 properties (firstName, lastName and rollNo) inside a component.
For example in the Student mapping you can define a StudentName component which contains the 3 properties.
Then you can directly query this component and you will get a List<StudentName> as result!


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 5:11 pm 
Newbie

Joined: Thu Dec 16, 2010 5:04 pm
Posts: 4
senthilkumara20 wrote:
Hi,

I face an issue with my query with some of the properties been specified in the query.

Eg: My query looks like,
SELECT student.firstName, student.lastName, student.rollNo FROM Student AS student

I assume, this query returns a list of Student with values been set to the properties specified in the query and all other properties to null reference.

But the result was a list of Object array with the values. Is there something wrong in my assumption Or Hibernate does not support such an option ?

If no support available, I would b interested to know the reason behind no providing such support.

Thanks,
Senthilkumar Arumugam


Please give your code block related with "the result was a list of Object array with the values".


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 9:29 pm 
Newbie

Joined: Mon Apr 11, 2011 4:19 am
Posts: 11
overmeulen wrote:
One solution would be to move the 3 properties (firstName, lastName and rollNo) inside a component.
For example in the Student mapping you can define a StudentName component which contains the 3 properties.
Then you can directly query this component and you will get a List<StudentName> as result!


Thank you, but it makes no much sense in our implementation. I build my query string dynamically, the number of select properties may vary for every method call.


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Mon Apr 11, 2011 9:36 pm 
Newbie

Joined: Mon Apr 11, 2011 4:19 am
Posts: 11
muzir wrote:
Please give your code block related with "the result was a list of Object array with the values".


I build my query dynamically at a PointCut, after generation my query would look like,

SELECT stud.firstName, stud.lastName, stud.rollNo, stud.dept.name FROM Student

Here, Student has many-to-one relation with Department.

Code:
final Query query = session.createQuery(queryStr);
final List<T> list = query.list();

return list;


I expected the list would be of Student with the values for properties firstName, lastName and rollNo been set and the value for name in Department in Student.

The actual result was a list of Object array.

Thanks,
Senthilkumar Arumugam


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Tue Apr 12, 2011 5:22 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Then the only solution I can think of is the one nordborg gave you: use the AliasToBean ResultTransformer
This solution is not well explained in the jboss doc. Here is a link with an example:

http://swik.net/Hibernate/Hibernate+Gro ... d+SQL/cmxs


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Tue Apr 12, 2011 6:26 am 
Newbie

Joined: Mon Apr 11, 2011 4:19 am
Posts: 11
overmeulen wrote:
Then the only solution I can think of is the one nordborg gave you: use the AliasToBean ResultTransformer
This solution is not well explained in the jboss doc. Here is a link with an example:

http://swik.net/Hibernate/Hibernate+Gro ... d+SQL/cmxs


Thank you, but I am finding hard luck yet in making use of ResultTransformer. I don't know what goes wrong with aliases, but I am getting exception when I build my properties with alias,
viewtopic.php?f=1&t=1010468

When I create the query without aliases for select properties, the query works fine but the result set is of type list of Object[]

Code:
final Query query = session.createQuery(queryStr);
query.setResultTransformer(new AliasToBeanRestultTransformer(Student.class));

return query.list();


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Tue Apr 12, 2011 6:46 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
You need aliases for the ResultTransformer to work because it uses reflexion based on those aliases ...
Which version of hibernate do you use?


Top
 Profile  
 
 Post subject: Re: Select properties query
PostPosted: Tue Apr 12, 2011 7:04 am 
Newbie

Joined: Mon Apr 11, 2011 4:19 am
Posts: 11
overmeulen wrote:
You need aliases for the ResultTransformer to work because it uses reflexion based on those aliases ...
Which version of hibernate do you use?


This is what my understanding too, but I am not sure how to move on when I get such an error with the query.

I use hibernate 3


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