-->
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.  [ 8 posts ] 
Author Message
 Post subject: Order by count of association
PostPosted: Mon Feb 07, 2011 3:00 am 
Newbie

Joined: Mon Feb 07, 2011 2:57 am
Posts: 5
Location: Eugene, Oregon
How would I order by count of a property list?

class Foo
{
@ManyToMany(targetEntity = Bar.class, cascade = CascadeType.ALL)
List<Bar> bars = ArrayList<Bar>();
}

I would like to get a list of Foo's ordered desc by bars.count

Any help would be much appreciated.
Thanks!

_________________
have faith in chaos


Top
 Profile  
 
 Post subject: Re: Order by count of association
PostPosted: Wed Feb 09, 2011 4:49 pm 
Newbie

Joined: Mon Feb 07, 2011 2:57 am
Posts: 5
Location: Eugene, Oregon
bump

_________________
have faith in chaos


Top
 Profile  
 
 Post subject: Re: Order by count of association
PostPosted: Thu Feb 10, 2011 7:24 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
Use the size function (4.6.17.2.2 Arithmetic Functions):

Code:
select f from Foo order by size(f.bars);


Top
 Profile  
 
 Post subject: Re: Order by count of association
PostPosted: Fri Feb 18, 2011 12:42 pm 
Newbie

Joined: Mon Feb 07, 2011 2:57 am
Posts: 5
Location: Eugene, Oregon
This works great in HQL!

Does anyone know about a Criteria Equivalent?
The queries I am working with are against a deep object graph, and allot of criteria has accumulated that must be re-coded in HQL just to use the Size() function.

Man, criteria solution would sure help...

_________________
have faith in chaos


Top
 Profile  
 
 Post subject: Re: Order by count of association
PostPosted: Sat Feb 19, 2011 9:55 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
Note that this is standard JPQL syntax (maybe it's HQL too), so the JPA Criteria equivalent would be
Quote:
CriteriaBuilder.size()

pretty easy isn't it?


Top
 Profile  
 
 Post subject: Re: Order by count of association
PostPosted: Wed Mar 02, 2011 4:37 am 
Newbie

Joined: Mon Feb 07, 2011 2:57 am
Posts: 5
Location: Eugene, Oregon
I am still having issue with this topic.
So, here are some more details so perhaps someone can give me literal syntax for my situation.

- Define Class Person and Shoe
- Add Collection to Person type called 'Shoes' that is a one-to-many of type Shoe

Criteria crit = session.createCriteria(Person.class);
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
crit.setFirstResult(pageNumber * pageSize);
crit.setMaxResults(pageSize);
result <- crit.list();

I would like to return a list that is sorted by the Count of Shoes per person, descending.
For instance,

Shoe Count | Person
5 Todd
4 Roger
1 Biff


Can someone show me an example of how I can achieve this?

_________________
have faith in chaos


Top
 Profile  
 
 Post subject: Re: Order by count of association
PostPosted: Thu Mar 03, 2011 8:51 pm 
Newbie

Joined: Mon Feb 07, 2011 2:57 am
Posts: 5
Location: Eugene, Oregon
Can any hibernate vetrans help with this?

_________________
have faith in chaos


Top
 Profile  
 
 Post subject: Re: Order by count of association
PostPosted: Fri Mar 04, 2011 7:21 am 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
Something like:
Code:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = cb.createQuery(Person.class);
Root<Person> person = criteria.from(Person.class);
criteria.select(person).orderBy(cb.desc(cb.size(person.get(Person_.shoes))));
TypedQuery<Person> query  = em.createQuery(criteria);
...


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