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

我已经使用toPredicate()方法完成了一些编码,现在我想重构它以便我Java 7也可以使用它。


我已经在下面发布了一些我到目前为止所做的示例代码。


EntitySpecification.java


public 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)

                );

            }

        };

    }


 }


慕雪6442864
浏览 135回答 2
2回答

森林海

Java 8 附带了lambda 表达式 ( ->)。为了使用 Java 7 中的代码,您必须将它们替换为匿名类。如果您使用像 IntelliJ 这样的 IDE,它可以为您完成这项工作。将光标移动到->,然后点击ALT + ENTER。应该会出现一个弹出窗口,并且应该有一个选项Replace lambda with anonymous class。.filter(a -> {&nbsp; &nbsp; if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }})到.filter(new java.util.function.Predicate<SingularAttribute<MyEntity, ?>>() {&nbsp; @Override&nbsp; public boolean test(SingularAttribute<MyEntity, ?> a) {&nbsp; &nbsp; &nbsp; if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }&nbsp; }})此外,您还必须从包中删除您正在使用的所有内容java.util.function。您可以将其替换.filter()为 for 循环和其中的 if 语句。为此,.map()您必须使用 for 循环修改先前过滤的集合。new Specification<MyEntity>() {&nbsp; @Override&nbsp; public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> cq, CriteriaBuilder builder) {&nbsp; &nbsp; &nbsp; List<SingularAttribute<MyEntity, ?>> tempAttributes = new ArrayList<>();&nbsp; &nbsp; &nbsp; for (SingularAttribute<MyEntity, ?> attribute : root.getModel().getDeclaredSingularAttributes()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (attribute.getJavaType().getSimpleName().equalsIgnoreCase("string")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempAttributes.add(attribute);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; final Predicate[] predicates = new Predicate[tempAttributes.size()];&nbsp; &nbsp; &nbsp; for (int i = 0; i < tempAttributes.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicates[i] = builder.like(root.<MyEntity>get(tempAttributes.get(i).getName()), finalText);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return builder.or(predicates);&nbsp; }};我自己没有尝试过,但这应该有效,或者至少给了你第一步。

呼啦一阵风

你必须更换-&nbsp;stream -&nbsp;filter -&nbsp;map因为 Java 7 中没有 Streaming API。通过替换我的意思是你必须遍历 getDeclaredSingularAttributes() 并过滤元素并映射它。也Predicate[]::new必须替换为new Predicate[]因为现在有方法引用。正如 Rashin 所说,如果将源代码级别设置为 Java 7,这可以通过 IDE 完成,它将提供帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java