2

How can I create a (human readable) string representation of Predicates, that shows me the logical operands AND/OR/NOT as well as the attributes and parameter placeholders involved?

There is no need for a SQL or platform specific transformation.

regards

Steve Oh
  • 1,149
  • 10
  • 18
  • this does a quite good job with EclipseLink provider: ((SelectionImpl>) predicate).getCurrentNode().toString() – Steve Oh Aug 29 '13 at 17:26

1 Answers1

1

This works fine for me with EclipseLink 2.3/2.4:

import org.eclipse.persistence.internal.jpa.querydef.SelectionImpl;

public static String getPredicateAsString(final Predicate predicate) {
    if (predicate == null) {
        return null;
    }
    if (!(predicate instanceof SelectionImpl<?>)) { // type guard
        return "not supported";
    }

    return ((SelectionImpl<?>) predicate).getCurrentNode().toString();
}

Sample output from getPredicateAsString:

Logical operator [ AND ]
    Relation operator [ <> ]
       Query Key age
          Base model.Person
       Constant 42
    Function operator [(,  IS NOT NULL)]
       Query Key currentJob
          Base model.Employer
Steve Oh
  • 1,149
  • 10
  • 18