Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate query problem

 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have an domain object Student with some attributes Id, first_name,last_name,contact_number etc. I want to get only first_name and last_name for all the students and return value should be list of student objects.


The following sql is returning list of student objects.




Where as the following sql is returning list of arrays. I want to get the result as List of student objects



If anyone knows how to get list of domain objects populated with first_name and last_name please let me know

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Hibernate will simply return fully populated instances. As you see, custom queries return arrays of data. You're basically demonstrating the two alternative here. I'm not sure if a third option exists.

You could create a new class that only has the first and last names and then map it to the db, and query based on this new object, but I think that's the best you're going to do.

-Cameron McKenzie
 
Venkata Kumar
Ranch Hand
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cameron.

-Venkat
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do something like this if appropriate constructor in class student exists:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic