diff --git a/src/main/java/com/avaje/ebeaninternal/api/ManyWhereJoins.java b/src/main/java/com/avaje/ebeaninternal/api/ManyWhereJoins.java index 8421c034b..bdc3208d3 100644 --- a/src/main/java/com/avaje/ebeaninternal/api/ManyWhereJoins.java +++ b/src/main/java/com/avaje/ebeaninternal/api/ManyWhereJoins.java @@ -53,13 +53,15 @@ public class ManyWhereJoins implements Serializable { String join = elProp.getElPrefix(); BeanProperty p = elProp.getBeanProperty(); if (p instanceof BeanPropertyAssocMany){ - join = addManyToJoin(join, p.getName()); + join = addManyToJoin(join, p.getName()); } if (join != null){ addJoin(join); - String secondaryTableJoinPrefix = p.getSecondaryTableJoinPrefix(); - if (secondaryTableJoinPrefix != null) { - addJoin(join+"."+secondaryTableJoinPrefix); + if (p != null) { + String secondaryTableJoinPrefix = p.getSecondaryTableJoinPrefix(); + if (secondaryTableJoinPrefix != null) { + addJoin(join+"."+secondaryTableJoinPrefix); + } } addParentJoins(join); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java index 28b2b621c..5143748d3 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptor.java @@ -1531,7 +1531,7 @@ public class BeanDescriptor implements MetaBeanInfo { if (propertyDeploy && chain != null) { BeanFkeyProperty fk = fkeyMap.get(propName); if (fk != null) { - return fk.create(chain.getExpression()); + return fk.create(chain.getExpression(), chain.isContainsMany()); } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanFkeyProperty.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanFkeyProperty.java index f60483f71..4f5ae3069 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanFkeyProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanFkeyProperty.java @@ -10,182 +10,188 @@ import com.avaje.ebeaninternal.server.el.ElPropertyValue; */ public final class BeanFkeyProperty implements ElPropertyValue { - private final String placeHolder; - private final String prefix; - private final String name; - private final String dbColumn; + private final String placeHolder; + private final String prefix; + private final String name; + private final String dbColumn; + private final boolean containsMany; - private int deployOrder; + private int deployOrder; - public BeanFkeyProperty(String prefix, String name, String dbColumn, int deployOrder) { - this.prefix = prefix; - this.name = name; - this.dbColumn = dbColumn; - this.deployOrder = deployOrder; - this.placeHolder = calcPlaceHolder(prefix, dbColumn); - } + public BeanFkeyProperty(String prefix, String name, String dbColumn, int deployOrder) { + this(prefix, name, dbColumn, deployOrder, false); + } + + private BeanFkeyProperty(String prefix, String name, String dbColumn, int deployOrder, boolean containsMany) { + this.prefix = prefix; + this.name = name; + this.dbColumn = dbColumn; + this.deployOrder = deployOrder; + this.containsMany = containsMany; + this.placeHolder = calcPlaceHolder(prefix, dbColumn); + } - public int getDeployOrder() { - return deployOrder; - } + public int getDeployOrder() { + return deployOrder; + } - private String calcPlaceHolder(String prefix, String dbColumn) { - if (prefix != null) { - return "${" + prefix + "}" + dbColumn; - } else { - return ROOT_ELPREFIX + dbColumn; - } + private String calcPlaceHolder(String prefix, String dbColumn) { + if (prefix != null) { + return "${" + prefix + "}" + dbColumn; + } else { + return ROOT_ELPREFIX + dbColumn; } + } - public BeanFkeyProperty create(String expression) { - int len = expression.length() - name.length() - 1; - String prefix = expression.substring(0, len); + public BeanFkeyProperty create(String expression, boolean containsMany) { + int len = expression.length() - name.length() - 1; + String prefix = expression.substring(0, len); - return new BeanFkeyProperty(prefix, name, dbColumn, deployOrder); - } + return new BeanFkeyProperty(prefix, name, dbColumn, deployOrder, containsMany); + } - /** - * Returns false for keys. - */ - public boolean isDbEncrypted() { - return false; - } + /** + * Returns false for keys. + */ + public boolean isDbEncrypted() { + return false; + } - /** - * Returns false for keys. - */ - public boolean isLocalEncrypted() { - return false; - } + /** + * Returns false for keys. + */ + public boolean isLocalEncrypted() { + return false; + } - /** - * Only usable as ElPropertyDeploy. - */ - public boolean isDeployOnly() { - return true; - } - - @Override - public boolean containsFormulaWithJoin() { - return false; - } + /** + * Only usable as ElPropertyDeploy. + */ + public boolean isDeployOnly() { + return true; + } - /** - * Returns false. - */ - public boolean containsMany() { - return false; - } - - public boolean containsManySince(String sinceProperty) { - return containsMany(); - } + @Override + public boolean containsFormulaWithJoin() { + return false; + } - public String getDbColumn() { - return dbColumn; - } + /** + * Returns false. + */ + public boolean containsMany() { + return containsMany; + } - public String getName() { - return name; - } + public boolean containsManySince(String sinceProperty) { + return containsMany(); + } - public String getElName() { - return name; - } + public String getDbColumn() { + return dbColumn; + } - /** - * Returns null as not an AssocOne. - */ - public Object[] getAssocOneIdValues(EntityBean value) { - return null; - } + public String getName() { + return name; + } - /** - * Returns null as not an AssocOne. - */ - public String getAssocOneIdExpr(String prefix, String operator) { - return null; - } + public String getElName() { + return name; + } - /** - * Returns null as not an AssocOne. - */ - public String getAssocIdInExpr(String prefix) { - return null; - } + /** + * Returns null as not an AssocOne. + */ + public Object[] getAssocOneIdValues(EntityBean value) { + return null; + } - /** - * Returns null as not an AssocOne. - */ - public String getAssocIdInValueExpr(int size) { - return null; - } + /** + * Returns null as not an AssocOne. + */ + public String getAssocOneIdExpr(String prefix, String operator) { + return null; + } - /** - * Returns false as not an AssocOne. - */ - public boolean isAssocId() { - return false; - } - - public boolean isAssocProperty() { - return false; - } + /** + * Returns null as not an AssocOne. + */ + public String getAssocIdInExpr(String prefix) { + return null; + } - public String getElPlaceholder(boolean encrypted) { - return placeHolder; - } + /** + * Returns null as not an AssocOne. + */ + public String getAssocIdInValueExpr(int size) { + return null; + } - public String getElPrefix() { - return prefix; - } + /** + * Returns false as not an AssocOne. + */ + public boolean isAssocId() { + return false; + } - public boolean isDateTimeCapable() { - return false; - } - - public int getJdbcType() { - return 0; - } + public boolean isAssocProperty() { + return false; + } - public Object parseDateTime(long systemTimeMillis) { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public String getElPlaceholder(boolean encrypted) { + return placeHolder; + } - public StringFormatter getStringFormatter() { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public String getElPrefix() { + return prefix; + } - public StringParser getStringParser() { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public boolean isDateTimeCapable() { + return false; + } - public void elSetReference(EntityBean bean) { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public int getJdbcType() { + return 0; + } - public Object elConvertType(Object value) { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public BeanProperty getBeanProperty() { + return null; + } - public void elSetValue(EntityBean bean, Object value, boolean populate) { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public Object parseDateTime(long systemTimeMillis) { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } - public Object elGetValue(EntityBean bean) { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public StringFormatter getStringFormatter() { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } - public Object elGetReference(EntityBean bean) { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public StringParser getStringParser() { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } - public BeanProperty getBeanProperty() { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public void elSetReference(EntityBean bean) { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } - public String getDeployProperty() { - throw new RuntimeException("ElPropertyDeploy only - not implemented"); - } + public Object elConvertType(Object value) { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } + + public void elSetValue(EntityBean bean, Object value, boolean populate) { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } + + public Object elGetValue(EntityBean bean) { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } + + public Object elGetReference(EntityBean bean) { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } + + public String getDeployProperty() { + throw new RuntimeException("ElPropertyDeploy only - not implemented"); + } } diff --git a/src/test/java/com/avaje/tests/query/TestManyWhereJoin.java b/src/test/java/com/avaje/tests/query/TestManyWhereJoin.java index 6e8a3aa13..577eea068 100644 --- a/src/test/java/com/avaje/tests/query/TestManyWhereJoin.java +++ b/src/test/java/com/avaje/tests/query/TestManyWhereJoin.java @@ -8,6 +8,7 @@ import com.avaje.ebean.Ebean; import com.avaje.ebean.Query; import com.avaje.tests.model.basic.Customer; import com.avaje.tests.model.basic.Order; +import com.avaje.tests.model.basic.Product; import com.avaje.tests.model.basic.ResetBasicData; public class TestManyWhereJoin extends BaseTestCase { @@ -70,4 +71,95 @@ public class TestManyWhereJoin extends BaseTestCase { Assert.assertTrue(sql.indexOf("join o_order u1 on ") > -1); Assert.assertTrue(sql.indexOf(" u1.status = ?") > -1); } + + @Test + public void testUsingForeignKey() { + + ResetBasicData.reset(); + + Long productId = 1L; + + Query query = Ebean.find(Order.class) + .where().eq("details.product.id", productId) + .orderBy("cretime asc"); + + query.findList(); + String sql = query.getGeneratedSql(); + + // select distinct t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t1.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7, t0.cretime + // from o_order t0 + // join o_customer t1 on t1.id = t0.kcustomer_id + // join o_order_detail u1 on u1.order_id = t0.id + // where u1.product_id = ? + // order by t0.cretime; --bind(1) + + Assert.assertTrue(sql.indexOf("select distinct t0.id c0, t0.status c1,") > -1); + Assert.assertTrue(sql.indexOf(" join o_order_detail u1 on u1.order_id = t0.id") > -1); + Assert.assertTrue(sql.indexOf(" where u1.product_id = ?") > -1); + } + + /** + * Same as previous test but use a reference bean. + */ + @Test + public void testUsingForeignKeyReferenceBean() { + + ResetBasicData.reset(); + + Product product = Ebean.getReference(Product.class, 1L); + + Query query = Ebean.find(Order.class) + //.fetch("details") + .where().eq("details.product", product) + .orderBy("cretime asc"); + + query.findList(); + String sql = query.getGeneratedSql(); + + // select distinct t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t1.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7, t0.cretime + // from o_order t0 + // join o_customer t1 on t1.id = t0.kcustomer_id + // join o_order_detail u1 on u1.order_id = t0.id + // where u1.product_id = ? + // order by t0.cretime + + Assert.assertTrue(sql.indexOf("select distinct t0.id c0, t0.status c1,") > -1); + Assert.assertTrue(sql.indexOf(" join o_order_detail u1 on u1.order_id = t0.id") > -1); + Assert.assertTrue(sql.indexOf(" where u1.product_id = ?") > -1); + } + + /** + * Additionally add a fetch join. + */ + @Test + public void testUsingForeignKeyAndFetch() { + + ResetBasicData.reset(); + + Product product = Ebean.getReference(Product.class, 1L); + + Query query = Ebean.find(Order.class) + .fetch("details") + .where().eq("details.product", product) + .orderBy("cretime asc"); + + query.findList(); + String sql = query.getGeneratedSql(); + + // select distinct t0.id c0, t0.status c1, t0.order_date c2, t0.ship_date c3, t2.name c4, t0.cretime c5, t0.updtime c6, t0.kcustomer_id c7, + // t1.id c8, t1.order_qty c9, t1.ship_qty c10, t1.unit_price c11, t1.cretime c12, t1.updtime c13, t1.order_id c14, t1.product_id c15, t0.cretime, t0.id, t1.id, t1.order_qty, t1.cretime + // from o_order t0 + // join o_customer t2 on t2.id = t0.kcustomer_id + // left outer join o_order_detail t1 on t1.order_id = t0.id + // join o_order_detail u1 on u1.order_id = t0.id + // where t1.id > 0 and u1.product_id = ? + // order by t0.cretime, t0.id, t1.id asc, t1.order_qty asc, t1.cretime desc; --bind(1) + + Assert.assertTrue(sql.indexOf("select distinct t0.id c0, t0.status c1,") > -1); + Assert.assertTrue(sql.indexOf(" join o_order_detail u1 on u1.order_id = t0.id") > -1); + Assert.assertTrue(sql.indexOf(" u1.product_id = ?") > -1); + + // additional join for fetching the many details + Assert.assertTrue(sql.indexOf(" left outer join o_order_detail t1 on t1.order_id = t0.id") > -1); + } }