From 6b54b6ef4662df244b0f2d106972065e9638c179 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Tue, 12 Jul 2016 16:11:51 +1200 Subject: [PATCH] Refactor BeanPropertyAssocOne findByParentIds use of raw expression --- .../server/deploy/BeanPropertyAssoc.java | 20 ++++++++++ .../server/deploy/BeanPropertyAssocMany.java | 19 --------- .../server/deploy/BeanPropertyAssocOne.java | 40 +++++++------------ 3 files changed, 34 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssoc.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssoc.java index 7083e727e..b9bc0bd92 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssoc.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssoc.java @@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory; import javax.persistence.PersistenceException; import java.util.ArrayList; +import java.util.List; /** * Abstract base for properties mapped to an associated bean, list, set or map. @@ -41,6 +42,11 @@ public abstract class BeanPropertyAssoc extends BeanProperty { String targetIdProperty; + /** + * Derived list of exported property and matching foreignKey + */ + protected ExportedProperty[] exportedProperties; + /** * Persist settings. */ @@ -385,4 +391,18 @@ public abstract class BeanPropertyAssoc extends BeanProperty { + " Perhaps an error in a @JoinColumn"; throw new PersistenceException(msg); } + + protected void bindWhereParentId(List bindValues, Object parentId) { + + if (exportedProperties.length == 1) { + bindValues.add(parentId); + + } else { + EntityBean parent = (EntityBean) parentId; + for (int i = 0; i < exportedProperties.length; i++) { + Object embVal = exportedProperties[i].getValue(parent); + bindValues.add(embVal); + } + } + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java index ee1e8bc70..4581ea350 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocMany.java @@ -85,11 +85,6 @@ public class BeanPropertyAssocMany extends BeanPropertyAssoc { private BeanProperty mapKeyProperty; - /** - * Derived list of exported property and matching foreignKey - */ - private ExportedProperty[] exportedProperties; - private String exportedPropertyBindProto = "?"; /** @@ -695,20 +690,6 @@ public class BeanPropertyAssocMany extends BeanPropertyAssoc { } } - private void bindWhereParentId(List bindValues, Object parentId) { - - if (exportedProperties.length == 1) { - bindValues.add(parentId); - - } else { - EntityBean parent = (EntityBean) parentId; - for (int i = 0; i < exportedProperties.length; i++) { - Object embVal = exportedProperties[i].getValue(parent); - bindValues.add(embVal); - } - } - } - public void addSelectExported(DbSqlContext ctx, String tableAlias) { String alias = manyToMany ? "int_" : tableAlias; diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java index ee70e29c9..20b2f56b2 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanPropertyAssocOne.java @@ -50,8 +50,6 @@ public class BeanPropertyAssocOne extends BeanPropertyAssoc { */ protected ImportedId importedId; - private ExportedProperty[] exportedProperties; - private String deleteByParentIdSql; private String deleteByParentIdInSql; @@ -240,47 +238,37 @@ public class BeanPropertyAssocOne extends BeanPropertyAssoc { String rawWhere = deriveWhereParentIdSql(false); + List bindValues = new ArrayList(); + bindWhereParentId(bindValues, parentId); + EbeanServer server = getBeanDescriptor().getEbeanServer(); Query q = server.find(getPropertyType()) - .where().raw(rawWhere).query(); + .where() + .raw(rawWhere, bindValues.toArray()) + .query(); - bindWhereParendId(q, parentId); return server.findIds(q, t); } - private List findIdsByParentIdList(List parentIdist, Transaction t) { + private List findIdsByParentIdList(List parentIdList, Transaction t) { String rawWhere = deriveWhereParentIdSql(true); - String inClause = targetIdBinder.getIdInValueExpr(parentIdist.size()); + String inClause = targetIdBinder.getIdInValueExpr(parentIdList.size()); String expr = rawWhere + inClause; + List bindValues = new ArrayList(); + for (int i = 0; i < parentIdList.size(); i++) { + bindWhereParentId(bindValues, parentIdList.get(i)); + } + EbeanServer server = getBeanDescriptor().getEbeanServer(); Query q = (Query) server.find(getPropertyType()) - .where().raw(expr); - - for (int i = 0; i < parentIdist.size(); i++) { - bindWhereParendId(q, parentIdist.get(i)); - } + .where().raw(expr, bindValues.toArray()); return server.findIds(q, t); } - private void bindWhereParendId(Query q, Object parentId) { - - if (exportedProperties.length == 1) { - q.setParameter(1, parentId); - - } else { - int pos = 1; - EntityBean parent = (EntityBean) parentId; - for (int i = 0; i < exportedProperties.length; i++) { - Object embVal = exportedProperties[i].getValue(parent); - q.setParameter(pos++, embVal); - } - } - } - void addFkey() { if (importedId != null) { importedId.addFkeys(name);