【问题标题】:How to determine if an Entity has a NamedQuery如何确定实体是否有 NamedQuery
【发布时间】:2012-10-18 15:59:42
【问题描述】:

再次,基于a problem I had the other day 发布和回答一个为他人谋福利的问题。希望这可以节省一些时间。我有点惊讶这不是 JPA 的 EntityManager 内置的。

我如何确定一个实体是否有带有特定名称的 NamedQuery 注释?

在尝试执行实体之前,我需要确定实体是否具有特定的@NamedQuery

【问题讨论】:

    标签: java jpa reflection annotations named-query


    【解决方案1】:

    当注解嵌套时,您可以通过获取注解的属性来获取它的子项。在@NamedQueries 的情况下,它是.value()

    public boolean hasNamedQuery(Class<?> clazz, String nameOfQuery) {
        boolean foundQueryByName = false;
        NamedQueries namedQueries = clazz.getAnnotation(NamedQueries.class);
        if (namedQueries != null && namedQueries.value() != null) {
            NamedQuery[] values = namedQueries.value();
            for (int i = 0; i < values.length && !foundQueryByName; i++) {
                foundQueryByName |= (nameOfQuery.equals(values[i].name()));
            }
        }
        return foundQueryByName;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      相关资源
      最近更新 更多