Assume the following object graph (an example):
person.contacts.address.city.name
person is the owning class.
contacts is a collection (one-to-many), city and address are many-to-one relationships.
name is a string attribute on city.
Now I would like the following:
Get all persons which contacts are in a specific city. It should return person objects
where ONLY contacts are included where the city name equals a specific string. So not all
contacts are loaded but a subset of them.
As far as I read the documentation, there are two ways for doing that:
1. Use filter-defs on the collection and set the filter on-demand by using enableFilter on
the session
2. Filter the collections after getting the person objects (with perhaps fully loaded contacts collection)
with createFilter on the session.
I'm unhappy with the two approaches. Using filter-defs COULD work for me if the WHOLE condition could be
changed at runtime - not only parameters in it. I need this, because the user is able to search in
different attributes for n-values with like. So I would have to dynamically create the where clause
(e.g. "where name like '%:n1%' or name like '%:n2%'"...). Another question regarding filter-defs is lazy loading
and different sessions. The documentation says, that the enableFilter works only for the current session. What
happens if I load the persons in one session with the filter enabled, and attach the persons to a different session.
Then I call getContacts. I assume, that the contacts won't be filtered because the filter isn't set on the new
session, right? BTW: the documentation is poor on this point...
The 2nd way, using createFilter on already loaded collections, isn't really a good solution for me either. That would
mean that I have a second collection of contacts parallel to the person.contacts collection. I can't navigate to
it through the person class. I also have to use the where clause for selecting specific contacts twice. One for the person
select and one for the collection filter. I would like to keep the object graph navigation...
So, is there a way to handle this nicely?
|