• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate - Retriving Custom Query Result

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bean named Offer. Which has been mapped on offer table. There are say 10 columns which are mapped on bean, accordingly.

Now, I have a query which is to be run on Offer table



This will return a resultset with 2 columns only, which are definitely not mapped on Offer Bean. Now, my question is what should be the best practice to retrieve data from the result and return? Hibernate will return raw objects or what?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate will return an array of Objects. You can deal with those yourself, for example you could in your DAO code turn these into a more usable type of object. Alternatively you could use the "select new" syntax and let Hibernate do this for you, e.g. :
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:Hibernate will return an array of Objects. You can deal with those yourself, for example you could in your DAO code turn these into a more usable type of object. Alternatively you could use the "select new" syntax and let Hibernate do this for you, e.g. :

in the case where i am using "new". will Hibernate return Offer objects or raw objects ?
 
John Bengler
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Hibernate will return the object which you create with "new", in the example of Paul Sturrock an OrderedOffer.

Of course you will have to write such a class and a matching constructor which takes the datatypes of the columns which are returned by the query.



John
 
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
Remember, the great thing about Hibernate is that it allows you to think about your objects as data.

A truely object oriented approach is to perform you query, and get objects back from the database. Then you use those objects in the application. Hibernate will create the objects for you if they are mapped naturally from the database to the object model. That will save you alot of time creating constructors based on database results.

Think of your problem domain as a set of objects, not just rows and columns in a database, and you'll be much more productive with Hibernate.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic