diff --git a/org.adempiere.base/src/org/compiere/model/Query.java b/org.adempiere.base/src/org/compiere/model/Query.java index 6d6eafd5d8..07212a1cf7 100644 --- a/org.adempiere.base/src/org/compiere/model/Query.java +++ b/org.adempiere.base/src/org/compiere/model/Query.java @@ -290,6 +290,19 @@ public class Query return this; } + /** + * Convenient method to add table direct type of joint.
+ * For e.g, if foreignTableName is C_BPartner and TableName for Query is AD_User,
+ * this will add join clause of
+ * "INNER JOIN C_BPartner ON (AD_User.C_BPartner_ID=C_BPartner.C_BParner_ID)". + * @param foreignTableName + */ + public void addTableDirectJoin(String foreignTableName) { + String foreignId = foreignTableName + "_ID"; + addJoinClause("INNER JOIN " + foreignTableName + " ON (" + table.getTableName() + "." + foreignId + + "=" + foreignTableName + "." + foreignId + ")"); + } + /** * Return a list of all po that match the query criteria. * @return List diff --git a/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java b/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java index 85756f3b4a..f62e6f6359 100644 --- a/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java +++ b/org.idempiere.test/src/org/idempiere/test/base/QueryTest.java @@ -48,6 +48,7 @@ import org.compiere.model.MPInstance; import org.compiere.model.MProcess; import org.compiere.model.MTable; import org.compiere.model.MTest; +import org.compiere.model.MUser; import org.compiere.model.PO; import org.compiere.model.POResultSet; import org.compiere.model.Query; @@ -56,6 +57,7 @@ import org.compiere.util.DB; import org.compiere.util.Env; import org.compiere.util.KeyNamePair; import org.idempiere.test.AbstractTestCase; +import org.idempiere.test.DictionaryIDs; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -421,4 +423,15 @@ public class QueryTest extends AbstractTestCase { assertEquals(expected, testRecord.getTestVirtualQty().setScale(2, RoundingMode.HALF_UP), "Wrong value returned"); } + @Test + public void testTableDirectJoin() { + Query query = new Query(Env.getCtx(), MUser.Table_Name, MUser.COLUMNNAME_AD_User_ID + "=?", getTrxName()); + query.addTableDirectJoin("C_BPartner"); + query.setParameters(DictionaryIDs.AD_User.GARDEN_USER.id); + MUser user = query.first(); + assertNotNull(user, "Failed to retrieve garden user record"); + + String sql = query.getSQL(); + assertTrue(sql.toLowerCase().contains("inner join c_bpartner on (ad_user.c_bpartner_id=c_bpartner.c_bpartner_id)"), "Unexpected SQL clause generated from query"); + } }