-->
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.  [ 6 posts ] 
Author Message
 Post subject: querying for the size of lists and sets
PostPosted: Thu Apr 20, 2006 2:56 pm 
Newbie

Joined: Thu Apr 20, 2006 2:50 pm
Posts: 3
i have a persistent object which has a set mapped to it. the mapping looks something like this:


<hibernate-mapping>
<class name="org.MyPeristentCollection" schema="MYSCHEMA" table="MyPersistentCollection">
<cache usage="read-write"/>
<id column="N_COLLECTION_ID" name="id" type="long">
<generator class="sequence">
<param name="sequence">MYSCHEMA.N_COLLECTION_ID_COUNTER</param>
</generator>
</id>

<set name="members" table="MYSCHEMA.COLLECTION_TO_MEMBER" lazy="true">
<cache usage="read-write"/>
<key column="N_COLLECTION_MEMBER_ID"/>
<many-to-many class="org.CollectionMember" column="N_COLLECTION_MEMBER_ID" />
</set>
</class>
</hibernate-mapping>

this works well. the problem i'm facing is i need a list of the sizes of these collections. something like:

select c.members.size from MyPersistentCollection c

but of course, size doesn't map to a mapped property. how can i fix this without loading the members set?


Top
 Profile  
 
 Post subject: Re: querying for the size of lists and sets
PostPosted: Thu Apr 20, 2006 3:08 pm 
Regular
Regular

Joined: Wed Feb 22, 2006 11:28 am
Posts: 65
Location: Santiago, Chile
Try this:
<query name="test">
select size(c.members) from MyPersistentCollection c
</query>


then your hibernate code:

query = session.getQuery("test");

int x = Integer.parseInt(String.valueOf(query.list().get(0)));


i guess that will help you.

request for comments.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:12 pm 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
This might help:

Code:
select count(org.MyPeristentCollection.members) from org.MyPeristentCollection


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:16 pm 
Newbie

Joined: Thu Apr 20, 2006 2:50 pm
Posts: 3
using either count() or size() gives me the aggregate size of all members. what i would like is a list of the individual size of each member set.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:20 pm 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Do you mean something like 'group by' MyPeristentCollection.id?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:25 pm 
Newbie

Joined: Thu Apr 20, 2006 2:50 pm
Posts: 3
to clarify what i'm trying to get...
for example:
if i had 3 collections, each with 10 members.
when i query for the sizes, i'd like to retrieve a list of integers with the values 10,10,10.


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