Explorar el Código

x_base_core_project fixed

zhourui hace 5 años
padre
commit
38257d694d

+ 10 - 15
o2server/x_base_core_project/src/main/java/com/x/base/core/container/EntityManagerContainer.java

@@ -10,6 +10,7 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Objects;
+import java.util.stream.Collectors;
 
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
@@ -244,7 +245,8 @@ public class EntityManagerContainer extends EntityManagerContainerBasic {
 			CriteriaQuery<T> cq = cb.createQuery(cls);
 			Root<T> root = cq.from(cls);
 			Predicate p = cb.equal(root.get(field.getName()), flag);
-			List<T> list = em.createQuery(cq.select(root).where(p).distinct(true)).setMaxResults(2).getResultList();
+			List<T> list = em.createQuery(cq.select(root).where(p)).setMaxResults(2).getResultList().stream().distinct()
+					.collect(Collectors.toList());
 			switch (list.size()) {
 			case 0:
 				break;
@@ -252,7 +254,7 @@ public class EntityManagerContainer extends EntityManagerContainerBasic {
 				t = list.get(0);
 				break out;
 			case 2:
-				throw new Exception("flag get multiple entity flag:" + flag + ", class:" + cls.getName()
+				throw new IllegalStateException("flag get multiple entity flag:" + flag + ", class:" + cls.getName()
 						+ ", attribute:" + field.getName() + ".");
 			}
 		}
@@ -265,7 +267,7 @@ public class EntityManagerContainer extends EntityManagerContainerBasic {
 
 	private <T extends JpaObject> List<T> flag(List<String> FLAGS, Class<T> cls, List<Field> fields) throws Exception {
 		if (ListTools.isEmpty(fields)) {
-			throw new Exception("attributes can not be empty.");
+			throw new IllegalStateException("attributes can not be empty.");
 		}
 		List<T> list = new ArrayList<>();
 		if (ListTools.isEmpty(FLAGS)) {
@@ -582,9 +584,7 @@ public class EntityManagerContainer extends EntityManagerContainerBasic {
 		CriteriaQuery<T> cq = cb.createQuery(cls);
 		Root<T> root = cq.from(cls);
 		cq.select(root).where(cb.isMember(root.get(attribute), cb.literal(values)));
-		List<T> os = em.createQuery(cq.distinct(true)).getResultList();
-		List<T> list = new ArrayList<>(os);
-		return list;
+		return new ArrayList<>(em.createQuery(cq).getResultList().stream().distinct().collect(Collectors.toList()));
 	}
 
 	public <T extends JpaObject> List<T> listIsMember(Class<T> cls, String attribute, Object value) throws Exception {
@@ -888,9 +888,8 @@ public class EntityManagerContainer extends EntityManagerContainerBasic {
 		CriteriaQuery<String> cq = cb.createQuery(String.class);
 		Root<T> root = cq.from(cls);
 		cq.select(root.get(JpaObject.id_FIELDNAME)).where(cb.isMember(root.get(attribute), cb.literal(values)));
-		List<String> os = em.createQuery(cq.distinct(true)).getResultList();
-		List<String> list = new ArrayList<>(os);
-		return list;
+		return new ArrayList<>(
+				em.createQuery(cq).getResultList().stream().distinct().collect(Collectors.toList()));
 	}
 
 	public <T extends JpaObject, W extends Object> List<String> idsNotIn(Class<T> cls, String attribute,
@@ -900,9 +899,7 @@ public class EntityManagerContainer extends EntityManagerContainerBasic {
 		CriteriaQuery<String> cq = cb.createQuery(String.class);
 		Root<T> root = cq.from(cls);
 		cq.select(root.get(JpaObject.id_FIELDNAME)).where(cb.not(root.get(attribute).in(values)));
-		List<String> os = em.createQuery(cq.distinct(true)).getResultList();
-		List<String> list = new ArrayList<>(os);
-		return list;
+		return new ArrayList<>(em.createQuery(cq).getResultList().stream().distinct().collect(Collectors.toList()));
 	}
 
 	public <T extends JpaObject, W extends Object> List<String> idsEqualAndNotIn(Class<T> cls, String attribute,
@@ -914,9 +911,7 @@ public class EntityManagerContainer extends EntityManagerContainerBasic {
 		Predicate p = cb.not(root.get(attribute).in(values));
 		p = cb.and(p, cb.equal(root.get(otherAttribute), otherValue));
 		cq.select(root.get(JpaObject.id_FIELDNAME)).where(p);
-		List<String> os = em.createQuery(cq.distinct(true)).getResultList();
-		List<String> list = new ArrayList<>(os);
-		return list;
+		return new ArrayList<>(em.createQuery(cq).getResultList().stream().distinct().collect(Collectors.toList()));
 	}
 
 	public <T extends JpaObject> List<String> idsIsMember(Class<T> cls, String attribute, Object value)

+ 5 - 79
o2server/x_base_core_project/src/main/java/com/x/base/core/project/jaxrs/StandardJaxrsAction.java

@@ -624,7 +624,7 @@ public abstract class StandardJaxrsAction extends AbstractJaxrsAction {
 			ListOrderedMap<String, Collection<?>> notIns, ListOrderedMap<String, Object> members,
 			ListOrderedMap<String, Object> notMembers, boolean andJoin, String order) throws Exception {
 		EntityManager em = emc.get(cls);
-		String str = "SELECT count(distinct o) FROM " + cls.getCanonicalName() + " o";
+		String str = "SELECT count(o) FROM " + cls.getCanonicalName() + " o";
 		/* 预编译的SQL语句的参数序号,必须由1开始 */
 		Integer index = 1;
 		List<String> ps = new ArrayList<>();
@@ -696,80 +696,6 @@ public abstract class StandardJaxrsAction extends AbstractJaxrsAction {
 		return (Long) query.getSingleResult() + 1;
 	}
 
-	// private <T extends JpaObject> Long count(EntityManagerContainer emc, Class<T>
-	// cls, EqualsTerms equals,
-	// NotEqualsTerms notEquals, LikeTerms likes, InTerms ins, NotInTerms notIns,
-	// MemberTerms members,
-	// NotMemberTerms notMembers, boolean andJoin) throws Exception {
-	// EntityManager em = emc.get(cls);
-	// String str = "SELECT count(distinct o) FROM " + cls.getCanonicalName() + "
-	// o";
-	// /* 预编译的SQL语句的参数序号,必须由1开始 */
-	// Integer index = 1;
-	// List<String> ps = new ArrayList<>();
-	// List<Object> vs = new ArrayList<>();
-	// if (null != equals && (!equals.isEmpty())) {
-	// for (Entry<String, Object> en : equals.entrySet()) {
-	// ps.add("o." + en.getKey() + (" = ?" + index));
-	// vs.add(en.getValue());
-	// index++;
-	// }
-	// }
-	// if (null != notEquals && (!notEquals.isEmpty())) {
-	// for (Entry<String, Object> en : notEquals.entrySet()) {
-	// ps.add("(o." + en.getKey() + (" <> ?" + index) + " or o." + en.getKey() + "
-	// is null)");
-	// vs.add(en.getValue());
-	// index++;
-	// }
-	// }
-	// if (null != likes && (!likes.isEmpty())) {
-	// List<String> ors = new ArrayList<>();
-	// for (Entry<String, Object> en : likes.entrySet()) {
-	// ors.add("o." + en.getKey() + (" Like ?" + index));
-	// vs.add("%" + en.getValue() + "%");
-	// index++;
-	// }
-	// ps.add("(" + StringUtils.join(ors, " or ") + ")");
-	// }
-	// if (null != ins && (!ins.isEmpty())) {
-	// for (Entry<String, Collection<?>> en : ins.entrySet()) {
-	// ps.add("o." + en.getKey() + (" in ?" + index));
-	// vs.add(en.getValue());
-	// index++;
-	// }
-	// }
-	// if (null != notIns && (!notIns.isEmpty())) {
-	// for (Entry<String, Collection<?>> en : notIns.entrySet()) {
-	// ps.add("o." + en.getKey() + (" not in ?" + index));
-	// vs.add(en.getValue());
-	// index++;
-	// }
-	// }
-	// if (null != members && (!members.isEmpty())) {
-	// for (Entry<String, Object> en : members.entrySet()) {
-	// ps.add(("?" + index) + (" member of o." + en.getKey()));
-	// vs.add(en.getValue());
-	// index++;
-	// }
-	// }
-	// if (null != notMembers && (!notMembers.isEmpty())) {
-	// for (Entry<String, Object> en : notMembers.entrySet()) {
-	// ps.add(("?" + index) + (" not member of o." + en.getKey()));
-	// vs.add(en.getValue());
-	// index++;
-	// }
-	// }
-	// if (!ps.isEmpty()) {
-	// str += " where " + StringUtils.join(ps, (andJoin ? " and " : " or "));
-	// }
-	// Query query = em.createQuery(str, cls);
-	// for (int i = 0; i < vs.size(); i++) {
-	// query.setParameter(i + 1, vs.get(i));
-	// }
-	// return (Long) query.getSingleResult();
-	// }
-
 	private <T extends JpaObject> Long count(EntityManagerContainer emc, Class<T> cls, EqualsTerms equals,
 			NotEqualsTerms notEquals, LikeTerms likes, InTerms ins, NotInTerms notIns, MemberTerms members,
 			NotMemberTerms notMembers, boolean andJoin) throws Exception {
@@ -1121,7 +1047,7 @@ public abstract class StandardJaxrsAction extends AbstractJaxrsAction {
 				selections.add(root.get(field));
 			}
 
-			List<Tuple> os = em.createQuery(cq.multiselect(selections).distinct(true))
+			List<Tuple> os = em.createQuery(cq.multiselect(selections))
 					.setMaxResults(Math.max(Math.min(count, list_max), list_min)).getResultList();
 
 			List<W> ws = new ArrayList<W>();
@@ -1181,7 +1107,7 @@ public abstract class StandardJaxrsAction extends AbstractJaxrsAction {
 				selections.add(root.get(field));
 			}
 
-			List<Tuple> os = em.createQuery(cq.multiselect(selections).distinct(true))
+			List<Tuple> os = em.createQuery(cq.multiselect(selections))
 					.setMaxResults(Math.max(Math.min(count, list_max), list_min)).getResultList();
 
 			List<W> ws = new ArrayList<W>();
@@ -1236,8 +1162,8 @@ public abstract class StandardJaxrsAction extends AbstractJaxrsAction {
 				cq.orderBy(cb.asc(root.get(sequenceField)));
 			}
 
-			List<T> os = em.createQuery(cq.select(root).distinct(true))
-					.setMaxResults(Math.max(Math.min(count, list_max), list_min)).getResultList();
+			List<T> os = em.createQuery(cq.select(root)).setMaxResults(Math.max(Math.min(count, list_max), list_min))
+					.getResultList();
 
 			ActionResult<List<T>> result = new ActionResult<>();
 			result.setData(new ArrayList<T>(os));