Search code examples
javajpa-2.0eclipselinkcriteria

JPA Criteria API: how to dump a human-readable representation for Predicates


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


Solution

  • 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