From 753843fa5f0bb8b76978d48d57c120e20b442aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E7=A8=B3?= Date: Fri, 16 Oct 2015 18:50:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8ESearchFilter=E7=9A=84DynamicS?= =?UTF-8?q?pecification=E6=9F=A5=E8=AF=A2=E6=B7=BB=E5=8A=A0=E5=A6=82?= =?UTF-8?q?=E4=B8=8B=E6=94=AF=E6=8C=81=EF=BC=9A=201.=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=202.=E5=AF=B9=E8=B1=A1=E7=BA=A7=E8=81=94?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../persistence/DynamicSpecifications.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/modules/core/src/main/java/org/springside/modules/persistence/DynamicSpecifications.java b/modules/core/src/main/java/org/springside/modules/persistence/DynamicSpecifications.java index 63421d953..5d7a99c85 100644 --- a/modules/core/src/main/java/org/springside/modules/persistence/DynamicSpecifications.java +++ b/modules/core/src/main/java/org/springside/modules/persistence/DynamicSpecifications.java @@ -8,11 +8,7 @@ import java.util.Collection; import java.util.List; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Path; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; +import javax.persistence.criteria.*; import org.apache.commons.lang3.StringUtils; import org.springframework.data.jpa.domain.Specification; @@ -33,11 +29,27 @@ public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuild // nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性 String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); - for (int i = 1; i < names.length; i++) { - expression = expression.get(names[i]); - } + for (int i = 1; i < names.length; i++) { + if (Collection.class.isAssignableFrom(expression.getJavaType())) { + Join parentJoin = null; + for (int j = 0; j < i; j++) { + if (j == 0) { + parentJoin = root.join(names[j]); + } else { + parentJoin = parentJoin.join(names[j]); + } + } + expression = parentJoin.get(names[i]); + } else { + expression = expression.get(names[i]); + } + } + + if (expression.getJavaType().isEnum()) { + filter.value = Enum.valueOf(expression.getJavaType(), filter.value.toString()); + } - // logic operator + // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value));