-->
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.  [ 2 posts ] 
Author Message
 Post subject: ClassCastException and HQL Query - issues with "distinct"
PostPosted: Sat May 02, 2009 9:00 am 
Newbie

Joined: Fri May 01, 2009 5:38 pm
Posts: 8
Hi,

I'm new to hibernate and have found a few issues that I was hoping I could maybe get some guidance for:

I'm using annotations and I've created a bean (DealerLocation) and mapped the database Columns to the corresponding variables in my bean. I have a method that returns a List<DealerLocation> for that bean, which executes an HQL query.

The following list works and returns a List populated with <DealerLocation> Objects, which is great:
locations = getSessionFactory().getCurrentSession().createQuery(
"from DealerLocation as locations order by locations.country, locations.state, locations.city").list();

However, if I modify the Query to use a select distinct instead (like this):

locations = getSessionFactory().getCurrentSession().createQuery(
"select distinct country, state, city from DealerLocation as locations order by locations.country, locations.state, locations.city").list();

I get the right number of records back (if I check locations.size()) but when I try and iterate through the List (using for (DealerLocation dl : locations) { ... }), I get this error:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.myapp.bean.DealerLocation

Am somewhat confused (a) as to why modifying the query suddenly breaks the casting; and (b) as to how I can iterate through the array of Objects[] to get my information into DealerLocation Objects (if I have to do that).

Could anyone here offer any advice / asssistance as to what I may be doing wrong? Or what I may need to do in order to get the correct results back?

Many thanks.
A


Top
 Profile  
 
 Post subject: Re: ClassCastException and HQL Query - issues with "distinct"
PostPosted: Sat May 02, 2009 3:21 pm 
Newbie

Joined: Fri May 01, 2009 5:38 pm
Posts: 8
I've found a workaround to get the values out.. Although it doesn't seem quite right...

List<HashMap> locations = getSessionFactory().getCurrentSession().createQuery(
"select distinct l.country, l.state, l.city from DealerLocation as l order by l.country, l.state, l.city")
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();

As I have been unsuccessful in being able to return a list of any specific type (e.g. DealerLocator), I noticed that there is a setResultTransformer() method that returns a HashMap for each item. So I have been able to iterate through the List that was returned and create and set the values accordingly. For example:

for (HashMap h : locations) {
System.out.println(h.get("0") + " | " + h.get("1") + " | " + h.get("2"));
// prints out the three values returned from my HQL query
}

Feels like a slightly incorrect use of hibernate, but at least it works.. Hope this helps someone else.


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