-->
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: HQL statement one-to-many - need some help
PostPosted: Mon Jun 13, 2011 6:21 pm 
Newbie

Joined: Mon Jun 13, 2011 6:19 pm
Posts: 4
Got a newbie HQL/SQL question for you all.

I have the following HQL statement

select count(ID) from Asset as asset where asset.StateInfo.MobileProvider.Name='Vodafone'

The Asset->StateInfo reference is one-to-many and the rest are one-to-one. This query generates a QueryException:

org.hibernate.QueryException: illegal attempt to dereference collection [asset0_.`id`.StateInfo] with element property reference [MobileProvider] [select count(ID) from Asset as asset where asset.StateInfo.MobileProvider.Name='Vodafone']

I'm sure this is because I'm doing the asset.StateInfo reference in the query all wrong. Would some kind soul please set me on the right path?

Cheers,
mike


Top
 Profile  
 
 Post subject: Re: HQL statement one-to-many - need some help
PostPosted: Tue Jun 14, 2011 3:03 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need to use a join:

Code:
...from Asset as asset join asset.StateInfo si where si.MobileProvider.....


Top
 Profile  
 
 Post subject: Re: HQL statement one-to-many - need some help
PostPosted: Fri Jun 24, 2011 6:49 pm 
Newbie

Joined: Mon Jun 13, 2011 6:19 pm
Posts: 4
Thanks very much! Unfortunately this seems to count all instances of MobileProvider. I'm trying to count only the last instance of StateInfo.MobileProvider.

Here is the exact query I tried:

Code:
select count(ID) from Asset as asset join asset.StateInfo si where si.MobileProvider.Name='Vodafone'


As you may recall I'm trying to count the number of instances where the last member/entry of Asset.StateInfo.MobileProvider.Name equals a given string. Here's a breakdown of the references

Asset--[one-to-many]-->StateInfo--[one-to-one]-->MobileProvider

Somebody else suggested using "elements" as such

Code:
select count(asset.ID) from Asset as asset, MobileProvider as mp where mp in elements(asset.StateInfo) and
mp.Name='Vodafone'


Unfortunately this appears to return the count of MobileProvider rows instead of the number of asset's that reference MobileProvider in the last member of StateInfo.

Any other suggestions would be greatly appreciated!

nordborg wrote:
You'll need to use a join:

Code:
...from Asset as asset join asset.StateInfo si where si.MobileProvider.....


Top
 Profile  
 
 Post subject: Re: HQL statement one-to-many - need some help
PostPosted: Fri Jun 24, 2011 6:56 pm 
Newbie

Joined: Mon Jun 13, 2011 6:19 pm
Posts: 4
Just figured it out. This works for me

Code:
select count(ID) from Asset as asset where asset.StateInfo[ maxindex(asset.StateInfo) ].MobileProvider.Name='%s'


Much more simple too!

mike


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.