简体   繁体   English

JPA Criteria API:如何为谓词转储人类可读的表示形式

[英]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? 如何创建Predicates的(人类可读的)字符串表示形式,向我显示逻辑操作数AND / OR / NOT以及所涉及的属性和参数占位符?

There is no need for a SQL or platform specific transformation. 无需进行SQL或平台特定的转换。

regards 问候

This works fine for me with EclipseLink 2.3/2.4: 这对于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 : 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM