aspose file tools*
The moose likes Object Relational Mapping and the fly likes Casting result of HQL query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Casting result of HQL query " Watch "Casting result of HQL query " New topic
Author

Casting result of HQL query

Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Hi to @ll, I'm posting my trouble after surfing in browsers searching about types conversion and casting... I had worked queries in hibernate using Criteria API, now I need to use HQL. When I used Criteria, I set result list to my plain class using: criteria.createCriteria(myclass.class), but I've tried to cast the result of query.list() to a myclass array through a for sentence:


But it throws java.lang.ClassCastException and I've couldn't to do the right casting.

Can I set the type in result of HQL query like I did in Criteria query?

Thanks in advance

[Edited to add code tags - Paul Sturrock]
[ October 12, 2006: Message edited by: Paul Sturrock ]

"La verdadera sabidur´┐Ża viene del temor de Dios"
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Typically HQL returns one of two things. This:

will return a list of Foos. However, this:

will return a list of Object arrays of properties, not a list of Foos.

Check you HQL and read more about it here.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Hi Paul, thanks for your reply...
Because I'm not retrieving a persistent class mapped in a mapping file, i'm retrieving an object, but can I get this object array like in Criteria setting the type: createCriteria(myClass)?

Thanks
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
talking in reference hibernate:

Queries may return multiple objects and/or properties as an array of type Object[],

select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr


But my plain class in which I want to retrieve the result isn't part of my mapping classes...

Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

If your query returns results that are not of a type of a mapped class you can always do a "Select new MyObject(1, 2, 3) from a, b, blah blah blah".

MyObject does not have to be mapped, but it requires that it has a constructor that takes the arguments selected, so in the above 1, 2, and 3.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Mark thanks for your reply, I seemed your solution is very well for me. I modified the myClass constructor to receive the needed parameters. But although this query ran before add:
select new myClass(a, b, c) from ...
now I'm getting this error message:



anyone got this message?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

In your new object class you have defined the constructor to take those parameters, and have a no-args constructor as well?

Might not be the solution, but just checking.

Mark
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
There is also another nice way of doing it, which does not require a huge constructor.

Use:



This way instead of using the constructor it uses the setters in the class.

John
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Thanks again Mark, I checked the constructor of my plain class, but it's taking my parameters, do you believe "select new Object" is implemented in newer versions of hibernate?, I'm not getting evidence of it.

Thanks John ...
I'm newbie in Hibernate, and I had not idea about
.setResultTransformer(Transformers.aliasToBean(SomeClass.class));

could I combine its use with HQL? I found examples, but these refer about Criteria API only javascript: x()
Confused
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Actually, now that you mention it, I do believe it is a 3.x version addition.

Mark
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Hi Mark, thanks for your reply, I'd like to know about for additions in distinct versions. Is there any public blog about it?

I'm using Hibernate3 and Oracle8i, but my HQL queries don't support I use alias in:
select C.prop1 as property from myClass c

Thanks in advance
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi,

I tend to use it with Criteria but you can use it with just normal HQL.

Here is a simple example:



Hope this helps?

John
[ October 13, 2006: Message edited by: John Bartlett ]
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Thanks John...
I had seen before the code:


in another post yours.

I think It's the solution I've been waiting, but I'm experimenting troubles with use alias when I write my query, I think it's a little problem between hibernate and my IDE. When I reach to do my query with aliases I will try with your code.

Bye
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Hi again guys, my trouble is resolved...
I've tested either solutions:

1)Mark suggested to create a new class constructor with properties in query projection as parameters.

2)John suggested to use .setResultTransformer(Transformers.aliasToBean(SomeClass.class))

Both solutions work for me... my problem was related with not find the class I want to use as DTO in runtime.

Thanks guys...
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hey Halcon,

Dont forget the Alias names you choose for the properties, i.e. using my example:



The alias has to match the name of the attribute in your new class. As using the Transform needs for find the setters for a given attribute so you must give it the name.

Post back if any of this is unclear,

John

John
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Good point John.

When using Hibernate 3.x or above, I think the projection with "new" in it looks cleaner than using the Transformer. What do you guys think?

Mark
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
I quite like the use of the seperate Transformer, just because i like the seperation of jobs.

The projections are used to pull back the required values (Select Statement) and then the Transfromer is used to create an object out of the projections.

I like this seperation.

John
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Hi again guys...
Thanks your posts were quite helpful.

I took the select new (par1, par2, par3) solution. For any reason the aliases don't work in my IDE, DataBase, Oracle combination, but I've resolved my trouble.
Julio Effgen
Greenhorn

Joined: Jul 16, 2013
Posts: 1
A great reference guide: http://relation.to/2133.lace
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting result of HQL query