-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to filter a collection of a an entity in one query?
PostPosted: Wed Nov 30, 2005 5:29 pm 
Newbie

Joined: Wed Aug 25, 2004 5:00 am
Posts: 4
Hi.

We're trying to do something similar to the following:
Support you have a forum with: topic 1-* posts.

Is it possible to query for topic, and return only a few posts? (for example: everything after yesterday) ?

In HQL you'd do something like:
select t, p from topic as t join t.posts as p where p.date < :date

Now, this works ok. You basically get a collection of tuples. To make it easier to work with, you could create a Map<Topic, List<Post>> by iterating over the tuples.

However, we'd like to work with your domain model.
So instead of the previous query, I'd like to do something like:
select t from topic as t fetch filtered t.posts as p where p.date < :date
or
select t, p as t.posts from topic as t join fetch t.posts as p where p.date < :date

So that I get a list of topics with only a few posts.

The only problem I see here, is that a Topic with id 1, would give back a different set of posts depending on which way it was queried. (all posts vs posts after yesterday), which can be confusing.

I read about virtualization, but iic it only allows class-level filtering, enabled by the session (not a query specific thing).

So:
* What is the 'best' solution for this kind of problem / how would you solve this?
* Is something like this possible in hibernate3?
* What other disadvantages would there be?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 6:03 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
look at (dynamic) filter in the reference guide

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 4:52 am 
Newbie

Joined: Wed Aug 25, 2004 5:00 am
Posts: 4
The part about filters (section 18) is not what we want, because it narrows all queries executed by the session. How are modifications handled to collections when using filters?


I also found the following (section16.4)
Code:
List cats = sess.createCriteria(Cat.class)
    .createCriteria("kittens", "kt")
        .add( Restrictions.eq("name", "F%") )
    .returnMaps()
    .list();
Iterator iter = cats.iterator();
while ( iter.hasNext() ) {
    Map map = (Map) iter.next();
    Cat cat = (Cat) map.get(Criteria.ROOT_ALIAS);
    Cat kitten = (Cat) map.get("kt");
}


This basically does the same as we already did.. (create a map.)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 5:47 am 
Newbie

Joined: Wed Aug 25, 2004 5:00 am
Posts: 4
kick


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 8:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Modifications to collections are always handled the same regardless of whether a filter is enabled/used or not. Not considering orphan deletes, such modifications come in two flavors:
1) a collection is "dereferenced" :
Code:
Order o = ( Order ) session.load( Order.class, orderId );
o.setLineItems( new HashSet() );
...

This causes Hibernate to "recreate" the collection (i.e. the original elements are deleted and any elements in the new collection reference are added).


2) elements are added or removed from a collection. Hibernate's Collection and Map impls keep track of the elements added and the elements removed, such that it can handle those individually at flush time.


Virtualization (aka filters) can be applied at either a class-level or collection-level.


BTW, the following HQ does essentially the same thing:
Code:
select t
from Topic as t
    join fetch t.posts as p
where p.date < :date

The where clause "filters" out the joined result set rows representing posts not meeting that criteria. Note, however, that this approach is questionable if you use second-level caching (then definitely use filters instead).


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