【问题标题】:JPA Criteria API: how to dump a human-readable representation for PredicatesJPA Criteria API:如何转储 Predicates 的人类可读表示
【发布时间】:2013-09-01 22:27:30
【问题描述】:

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

不需要特定于 SQL 或平台的转换。

问候

【问题讨论】:

  • 这对 EclipseLink 提供程序做得很好:((SelectionImpl>) predicate).getCurrentNode().toString()

标签: java jpa-2.0 eclipselink criteria


【解决方案1】:

这对我来说适用于 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

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2013-06-21
    • 2021-10-13
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多