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.  [ 5 posts ] 
Author Message
 Post subject: Retrieving query results
PostPosted: Mon Apr 13, 2009 12:30 pm 
Newbie

Joined: Mon Apr 13, 2009 12:03 pm
Posts: 4
Hello!

I have a program that generates HQL queries.

Since these queries are generated, their results are also variable. I cannot do as stated in most tutorials, where people do like this:


Code:
List results = q.list();
(PojoClass) results.get(i);
//do something with the object...


The problem is that each row of the results list is a composite object, with fields from multiple entity classes. A generated HQL query can be like this:

Code:
select place.location, car.license_plate
from Place place, Car car
where car.license_plate = 'foo'


I want to iterate through each row of my query; for each column of the row, figure out which hibernate type, cast it and and print out the contents.


I am very sorry if this has already been answered, but i've been searching for 2 days and still haven't figured it out :(

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 5:37 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Quote:
I want to iterate through each row of my query; for each column of the row, figure out which hibernate type, cast it and and print out the contents.

There should be nothing stopping you from just iterating over the list of objects that is returned and then creating any objects out of those results that you want?

If you prefer you can even use this syntax to create a map out of it.

Code:
select new java.util.HashMap(place.location as location, car.license_plate as licensePlate)
from Place place, Car car
where car.license_plate = 'foo'

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 14, 2009 4:28 am 
Newbie

Joined: Mon Apr 13, 2009 12:03 pm
Posts: 4
ethnarch wrote:
There should be nothing stopping you from just iterating over the list of objects that is returned and then creating any objects out of those results that you want?


Thank you very much for your answer! The problem is that I don't know which type to cast each column to, so I cannot create any new object from the results :(

The query result is composed of many rows. Each of these rows has some columns. How can I know which data type is in each column, programatically?

What I wanted was something like this (pseudo-Java :P) :

Code:
//compose Query q...
List results = q.list();
types = q.getColumnTypes();
for(row in results) do
   for(i=0; i<row.columns.length;i++) do
      print ( [b](types.get[i])[/b] row[i] ); //note the [b]cast[/b] inside the print()
   end
end


ethnarch wrote:
If you prefer you can even use this syntax to create a map out of it.

Code:
select new java.util.HashMap(place.location as location, car.license_plate as licensePlate)
from Place place, Car car
where car.license_plate = 'foo'


This idea is pretty nice! However, how do I know which object type to cast to when I call the map.get(location) ?

Like this:
Code:
(type?) map.get(location)


Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 14, 2009 7:52 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
you can check the type with the instanceof operator.

Otherwise you might have to build up or retain some metadata to map a field named "location" to the correct type or even just to a handler that can interpret your location piece of data. I don't think hibernate has the ability to help you at this point so you'll have to devise an efficient scheme yourself.

I also have a project that generates dynamic queries but lucky for me I only expect a few datatypes back and I know what types they are going to be. In this same project I have a user definable validation and I use something similar to the handler method above. Depending on the validation rule I will find the correct handler for that data type and and I will pass the raw object to it. The handler expects a certain datatype. I have more constrained rules around what I am doing so it's a little easier in my case.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 4:31 am 
Newbie

Joined: Mon Apr 13, 2009 12:03 pm
Posts: 4
It is a real pity that hibernate does not support accessing rows and casting each element to a specific type :S

I decided therefore to change my program to do several queries for each data type I wanted... not an elegant way but it works.

Thank you for your time, i've credited your replies ;)


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