如何创建谓词的(人类可读的)字符串表示形式,以显示逻辑操作数AND / OR / NOT以及所涉及的属性和参数占位符?

无需进行SQL或平台特定的转换。

问候

最佳答案

这对于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();
}


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

08-28 08:49
查看更多