亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重構 JPA 的 toPredicate 方法以支持 Java 7 和 8

重構 JPA 的 toPredicate 方法以支持 Java 7 和 8

慕雪6442864 2023-02-16 16:42:30
我已經使用toPredicate()方法完成了一些編碼,現在我想重構它以便我Java 7也可以使用它。我已經在下面發布了一些我到目前為止所做的示例代碼。EntitySpecification.javapublic class EntitySpecification {    public static Specification<MyEntity> textInAllColumns(String text) {        if (!text.contains("%")) {            text = "%"+text+"%";        }        final String finalText = text;        return new Specification<MyEntity>() {            @Override            public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> cq, CriteriaBuilder builder) {                return builder.or(root.getModel().getDeclaredSingularAttributes().stream().filter(a-> {                    if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {                        return true;                    }                    else {                        return false;                }}).map(a -> builder.like(root.get(a.getName()), finalText)                    ).toArray(Predicate[]::new)                );            }        };    } }
查看完整描述

2 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

Java 8 附帶了lambda 表達式 ( ->)。為了使用 Java 7 中的代碼,您必須將它們替換為匿名類。

如果您使用像 IntelliJ 這樣的 IDE,它可以為您完成這項工作。將光標移動到->,然后點擊ALT + ENTER。應該會出現一個彈出窗口,并且應該有一個選項Replace lambda with anonymous class。


.filter(a -> {

    if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {

        return true;

    } else {

        return false;

    }

})


.filter(new java.util.function.Predicate<SingularAttribute<MyEntity, ?>>() {

  @Override

  public boolean test(SingularAttribute<MyEntity, ?> a) {

      if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {

          return true;

      } else {

          return false;

      }

  }

})

此外,您還必須從包中刪除您正在使用的所有內容java.util.function。


您可以將其替換.filter()為 for 循環和其中的 if 語句。為此,.map()您必須使用 for 循環修改先前過濾的集合。


new Specification<MyEntity>() {

  @Override

  public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> cq, CriteriaBuilder builder) {

      List<SingularAttribute<MyEntity, ?>> tempAttributes = new ArrayList<>();

      for (SingularAttribute<MyEntity, ?> attribute : root.getModel().getDeclaredSingularAttributes()) {

          if (attribute.getJavaType().getSimpleName().equalsIgnoreCase("string")) {

              tempAttributes.add(attribute);

          }

      }


      final Predicate[] predicates = new Predicate[tempAttributes.size()];


      for (int i = 0; i < tempAttributes.size(); i++) {

          predicates[i] = builder.like(root.<MyEntity>get(tempAttributes.get(i).getName()), finalText);

      }


      return builder.or(predicates);

  }

};

我自己沒有嘗試過,但這應該有效,或者至少給了你第一步。


查看完整回答
反對 回復 2023-02-16
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

你必須更換

- stream
- filter
- map

因為 Java 7 中沒有 Streaming API。

通過替換我的意思是你必須遍歷 getDeclaredSingularAttributes() 并過濾元素并映射它。

Predicate[]::new必須替換為new Predicate[]因為現在有方法引用。

正如 Rashin 所說,如果將源代碼級別設置為 Java 7,這可以通過 IDE 完成,它將提供幫助。


查看完整回答
反對 回復 2023-02-16
  • 2 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號