0

According to EclipseLink/Examples/JPA/MappingSelectionCriteria I can make some filtering on OneToOne or OneToMany relationships. To do that I have to implement DescriptorCustomizer.

My question is: Can I do some conditional filtering with this technique and how? I mean, in the example of mentioned link we can write something like this

public class ConfigureBsFilter implements DescriptorCustomizer {

    public void customize(ClassDescriptor descriptor) throws Exception {
        OneToManyMapping mapping = (OneToManyMapping) descriptor
                .getMappingForAttributeName("bs");

        ExpressionBuilder eb = new ExpressionBuilder(mapping
                .getReferenceClass());
        Expression fkExp = eb.getField("A_ID").equal(eb.getParameter("A_ID"));
        Expression activeExp = eb.get("active").equal(true);

        mapping.setSelectionCriteria(fkExp.and(activeExp));
    }
}

But what if in the expression

Expression activeExp = eb.get("active").equal(true);

the "active" is not always true but have to be set at runtime by some parameter. Can I do that and how?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1278890
  • 643
  • 8
  • 22
  • This has bad idea written all over it. Once done, your entity will no longer represent what is in the database, and you can not be sure what you are merging into the context - which filter the entity was read using. It is usually better to leave it unmapped or lazy and never accessed and query for the referenced objects using the filter as needed. – Chris Apr 12 '13 at 18:40

1 Answers1

0

Looking at wiki.eclipse.org/Using_Advanced_Query_API_(ELUG) you could use a query redirector on the ForeignReferenceMapping#getSelectionQuery() so that your query redirector can dynamically clone the query and add filters as required. Passing parameters to the redirector will need to be creative though, such as storing them on the thread context or in the session's properties map.

perissf
  • 15,979
  • 14
  • 80
  • 117
Chris
  • 20,138
  • 2
  • 29
  • 43
  • Thanks for advice, but it seems to me a little 'hacky' and complicated. Also I've realized I can leave without it and just make a separate call for my dependency. So first I get the collection of my 'Main' objects, then I just make a separate call for a collection of dependent objects and then just iterate over 'Main' objects and populate needed transient property from the second collection in the loop. Also this way I think I will not brake any cache consistency and stuff, though maybe it is not so optimal solution. – user1278890 Apr 15 '13 at 08:16