-->
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.  [ 4 posts ] 
Author Message
 Post subject: illegal attempt to dereference collection
PostPosted: Mon Apr 06, 2009 1:30 pm 
Newbie

Joined: Mon Apr 06, 2009 1:19 pm
Posts: 2
Hello everyone,

I have three classes: Department, County, and Zipcode. Each Department is mapped to a set of counties that are associated with it, and each County has a set of mapped Zipcodes.

I am having difficulty getting a Department by Zipcode. The following HQL gives an error:

from Department as h where h.counties.id = (select c.id from County as c where c.zipcodes.id = (select z.ip from Zipcode as z where z.zipcode = '"+zipcode+"'))"

I've also tried:

from Department as h where h.counties.zipcodes.zipcode=zipcode

with same result. The error I get is:

"illegal attempt to dereference collection".

Ultimately, I've realized that where i am having trouble is manipulating the collections/set i get returned using h.counties or c.zipcodes.

Any help will be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 06, 2009 2:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Implicit join doesn't work with collections. You need to do an explicitly join. For example, something like this:

Code:
from Department as h
join h.counties c
join c.zipcodes z
where z.zipcode=zipcode


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 06, 2009 4:17 pm 
Newbie

Joined: Mon Apr 06, 2009 1:19 pm
Posts: 2
nordborg wrote:
Implicit join doesn't work with collections. You need to do an explicitly join. For example, something like this:

Code:
from Department as h
join h.counties c
join c.zipcodes z
where z.zipcode=zipcode


Thanks. That made some progress. However, I now get a java.lang.ClassCastException thrown when i do:
department = l.get(0);

The idea is to get a department object. Sounds like the query is not returning just a department object.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 06, 2009 4:34 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Oh.. I'm sorry. I tend to forget that when you use explicit joins the query returns all joined entities as well in an Object[]. If you only need the Department you need to specify what to select:

Code:
select h from Department h ....


In the original form you would get an Object[] of length 3 with:

department = a[0], county = a[1] and zipcode = a[2]


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