• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting result of HQL query

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Halcon Guatemala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Halcon Guatemala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, now that you mention it, I do believe it is a 3.x version addition.

Mark
 
Halcon Guatemala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A great reference guide: http://relation.to/2133.lace
reply
    Bookmark Topic Watch Topic
  • New Topic