From: Anton v. S. <an...@ap...> - 2002-09-13 05:45:24
|
> Accordingly Ive added filtering > methods to the Session interface like: > > filteredCollection = session.filter(collection, query) > > A filter is just a query where you can refer to "this", the collection > element. So, for example: > > blackCats = session.filter( cat.getKittens(), "where this.color='black' > order by this.name" ); Slightly tangential question about this: how straightforward (or not) would it be to support an object as the filter parameter? I use a Predicate interface in my own code: public interface Predicate { // Apply predicate to a specified object and return boolean result boolean apply(Object o); } This can be used to create arbitrary objects to filter on any condition - I use it for filtering Java collections and arrays. You can also use it inline, e.g.: blackCats = session.filter( cat.getKittens(), new Predicate() { public boolean apply(Object o) { return o.color.equals("black"); } }); I don't know anything about what Hibernate is doing when it's retrieving & filtering a query - if something like this wouldn't require too much internal rearranging, I might take a stab at it, since it would fit in with some of the other code I'm using. In fact it came up in some code which I've just installed for testing, and I ended up building a query string to do it instead. In some cases, I prefer the approach I've described since it's subject to static checking, supports refactoring, etc. Anton |