diff --git a/core/impl/src/main/java/com/blazebit/persistence/impl/JoinManager.java b/core/impl/src/main/java/com/blazebit/persistence/impl/JoinManager.java index cdf43be882..62bc22bafe 100644 --- a/core/impl/src/main/java/com/blazebit/persistence/impl/JoinManager.java +++ b/core/impl/src/main/java/com/blazebit/persistence/impl/JoinManager.java @@ -522,6 +522,7 @@ void setSubqueryInitFactory(SubqueryInitiatorFactory subqueryInitFactory) { } Set buildClause(StringBuilder sb, Set clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes, boolean externalRepresenation) { + final boolean renderFetches = !clauseExclusions.contains(ClauseType.SELECT); collectionJoinNodes.clear(); renderedJoins.clear(); sb.append(" FROM "); @@ -582,11 +583,11 @@ Set buildClause(StringBuilder sb, Set clauseExclusions, St // TODO: not sure if needed since applyImplicitJoins will already invoke that rootNode.registerDependencies(); - applyJoins(sb, rootNode.getAliasInfo(), rootNode.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes); + applyJoins(sb, rootNode.getAliasInfo(), rootNode.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches); if (!rootNode.getEntityJoinNodes().isEmpty()) { // TODO: Fix this with #216 boolean isCollection = true; - applyJoins(sb, rootNode.getAliasInfo(), new ArrayList(rootNode.getEntityJoinNodes()), isCollection, clauseExclusions, aliasPrefix, collectCollectionJoinNodes); + applyJoins(sb, rootNode.getAliasInfo(), new ArrayList(rootNode.getEntityJoinNodes()), isCollection, clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches); } } @@ -628,7 +629,7 @@ public void applyTransformer(ExpressionTransformer transformer) { } } - private void renderJoinNode(StringBuilder sb, JoinAliasInfo joinBase, JoinNode node, String aliasPrefix) { + private void renderJoinNode(StringBuilder sb, JoinAliasInfo joinBase, JoinNode node, String aliasPrefix, boolean renderFetches) { if (!renderedJoins.contains(node)) { switch (node.getJoinType()) { case INNER: @@ -641,7 +642,7 @@ private void renderJoinNode(StringBuilder sb, JoinAliasInfo joinBase, JoinNode n sb.append(" RIGHT JOIN "); break; } - if (node.isFetch()) { + if (node.isFetch() && renderFetches) { sb.append("FETCH "); } @@ -704,9 +705,9 @@ private void renderParentAlias(StringBuilder sb, JoinNode parentNode, String ali } } - private void renderReverseDependency(StringBuilder sb, JoinNode dependency, String aliasPrefix) { + private void renderReverseDependency(StringBuilder sb, JoinNode dependency, String aliasPrefix, boolean renderFetches) { if (dependency.getParent() != null) { - renderReverseDependency(sb, dependency.getParent(), aliasPrefix); + renderReverseDependency(sb, dependency.getParent(), aliasPrefix, renderFetches); if (!dependency.getDependencies().isEmpty()) { markedJoinNodes.add(dependency); try { @@ -716,27 +717,27 @@ private void renderReverseDependency(StringBuilder sb, JoinNode dependency, Stri + dep.getAliasInfo().getAbsolutePath() + "] with alias [" + dep.getAliasInfo().getAlias() + "]"); } // render reverse dependencies - renderReverseDependency(sb, dep, aliasPrefix); + renderReverseDependency(sb, dep, aliasPrefix, renderFetches); } } finally { markedJoinNodes.remove(dependency); } } - renderJoinNode(sb, dependency.getParent().getAliasInfo(), dependency, aliasPrefix); + renderJoinNode(sb, dependency.getParent().getAliasInfo(), dependency, aliasPrefix, renderFetches); } } - private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, Map nodes, Set clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes) { + private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, Map nodes, Set clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes, boolean renderFetches) { for (Map.Entry nodeEntry : nodes.entrySet()) { JoinTreeNode treeNode = nodeEntry.getValue(); List stack = new ArrayList(); stack.addAll(treeNode.getJoinNodes().descendingMap().values()); - applyJoins(sb, joinBase, stack, treeNode.isCollection(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes); + applyJoins(sb, joinBase, stack, treeNode.isCollection(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches); } } - private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List stack, boolean isCollection, Set clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes) { + private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List stack, boolean isCollection, Set clauseExclusions, String aliasPrefix, boolean collectCollectionJoinNodes, boolean renderFetches) { while (!stack.isEmpty()) { JoinNode node = stack.remove(stack.size() - 1); // If the clauses in which a join node occurs are all excluded or the join node is not mandatory for the cardinality, we skip it @@ -748,7 +749,7 @@ private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List // We have to render any dependencies this join node has before actually rendering itself if (!node.getDependencies().isEmpty()) { - renderReverseDependency(sb, node, aliasPrefix); + renderReverseDependency(sb, node, aliasPrefix, renderFetches); } // Collect the join nodes referring to collections @@ -757,11 +758,11 @@ private void applyJoins(StringBuilder sb, JoinAliasInfo joinBase, List } // Finally render this join node - renderJoinNode(sb, joinBase, node, aliasPrefix); + renderJoinNode(sb, joinBase, node, aliasPrefix, renderFetches); // Render child nodes recursively if (!node.getNodes().isEmpty()) { - applyJoins(sb, node.getAliasInfo(), node.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes); + applyJoins(sb, node.getAliasInfo(), node.getNodes(), clauseExclusions, aliasPrefix, collectCollectionJoinNodes, renderFetches); } } } diff --git a/core/testsuite/src/test/java/com/blazebit/persistence/testsuite/PaginationTest.java b/core/testsuite/src/test/java/com/blazebit/persistence/testsuite/PaginationTest.java index 8d88d00b37..6ca5426620 100644 --- a/core/testsuite/src/test/java/com/blazebit/persistence/testsuite/PaginationTest.java +++ b/core/testsuite/src/test/java/com/blazebit/persistence/testsuite/PaginationTest.java @@ -155,6 +155,20 @@ public void simpleTest() { assertEquals("DOC5", result.get(0).getName()); } + + @Test + public void simpleTestFetch() { + PaginatedCriteriaBuilder cb = cbf.create(em, Document.class, "d") + .where("d.name").like(true).value("doc%").noEscape() + .where("owner.name").eq("Karl1") + .orderByAsc("d.id") + .fetch("owner") + .page(0, 1); + List result = cb.getResultList(); + assertEquals(1, result.size()); + assertEquals("doc1", result.get(0).getName()); + } + @Test public void testSelectIndexedWithParameter() { String expectedCountQuery = "SELECT " + countPaginated("d.id", false) + " FROM Document d JOIN d.owner owner_1 WHERE owner_1.name = :param_0";