Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#190 - Test and fix for fetch joins with scalar selects #389

Merged
merged 1 commit into from
Mar 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Not yet released
* Support enum and entity type literal like the JPA spec says
* Introduction of `@MappingCorrelatedSimple` for simple correlations
* Allow empty correlation result with `JOIN` fetch strategy
* Support for join fetching with scalar selects

### Bug fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public abstract class AbstractCommonQueryBuilder<QueryResultType, BuilderType, S
protected String cachedExternalQueryString;
protected boolean hasGroupBy = false;
protected boolean needsCheck = true;
// Fetch owner's are evaluated during implicit joining
protected Map<JoinNode, Boolean> fetchOwners = new HashMap<>();

private boolean checkSetBuilderEnded = true;
private boolean implicitJoinsApplied = false;
Expand Down Expand Up @@ -1392,6 +1394,12 @@ public void visit(JoinNode node) {
selectManager.acceptVisitor(joinVisitor);
joinVisitor.setJoinRequired(true);

// Only the main query does has fetch owners
if (isMainQuery) {
fetchOwners.clear();
selectManager.collectFetchOwners(fetchOwners);
}

joinVisitor.setFromClause(ClauseType.WHERE);
whereManager.acceptVisitor(joinVisitor);
joinVisitor.setFromClause(ClauseType.GROUP_BY);
Expand Down Expand Up @@ -1818,7 +1826,7 @@ protected void appendSelectClause(StringBuilder sbSelectFrom) {

protected List<String> appendFromClause(StringBuilder sbSelectFrom, boolean externalRepresentation) {
List<String> whereClauseConjuncts = new ArrayList<>();
joinManager.buildClause(sbSelectFrom, EnumSet.noneOf(ClauseType.class), null, false, externalRepresentation, whereClauseConjuncts, explicitVersionEntities);
joinManager.buildClause(sbSelectFrom, EnumSet.noneOf(ClauseType.class), null, false, externalRepresentation, whereClauseConjuncts, explicitVersionEntities, fetchOwners);
return whereClauseConjuncts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,10 @@ private void checkEntityId(Object entityId) {
return (FullQueryBuilder<Y, ?>) this;
}

private void checkFetchJoinAllowed() {
if (selectManager.getSelectInfos().size() > 0) {
throw new IllegalStateException("Fetch joins are only possible if the root entity is selected");
}
}

@Override
@SuppressWarnings("unchecked")
public X fetch(String path) {
prepareForModification();
checkFetchJoinAllowed();
verifyBuilderEnded();
joinManager.implicitJoin(expressionFactory.createPathExpression(path), true, null, null, false, false, true, true);
return (X) this;
Expand All @@ -196,7 +189,6 @@ public X fetch(String path) {
@SuppressWarnings("unchecked")
public X fetch(String... paths) {
prepareForModification();
checkFetchJoinAllowed();
verifyBuilderEnded();

for (String path : paths) {
Expand Down Expand Up @@ -262,10 +254,6 @@ private X join(String path, String alias, JoinType type, boolean fetch, boolean
throw new IllegalArgumentException("Empty alias");
}

if (fetch == true) {
checkFetchJoinAllowed();
}

verifyBuilderEnded();
joinManager.join(path, alias, type, fetch, defaultJoin);
return (X) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Set<Expression> getCorrelatedExpressions() {
public T getResult() {
return result;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert

protected BaseFinalSetOperationSubqueryBuilderImpl<T, ?> createFinalSetOperationBuilder(SetOperationType operator, boolean nested, boolean isSubquery) {
SubqueryBuilderImpl<?> newInitiator = finalSetOperationBuilder == null ? null : finalSetOperationBuilder.getInitiator();
return createFinalSetOperationBuilder(operator, nested, isSubquery, newInitiator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public FinalSetOperationCTECriteriaBuilderImpl(MainQuery mainQuery, Class<T> cla
super(mainQuery, clazz, result, operator, nested, listener, initiator);
}

@Override
protected void applyImplicitJoins() {
// There is nothing to do here for final builders as they don't have any nodes
}

@Override
public T end() {
subListener.verifyBuilderEnded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public class FinalSetOperationCriteriaBuilderImpl<T> extends BaseFinalSetOperati
public FinalSetOperationCriteriaBuilderImpl(MainQuery mainQuery, boolean isMainQuery, Class<T> clazz, SetOperationType operator, boolean nested, BuilderListener<Object> listener) {
super(mainQuery, isMainQuery, clazz, operator, nested, listener, null);
}

@Override
protected void applyImplicitJoins() {
// There is nothing to do here for final builders as they don't have any nodes
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public FinalSetOperationSubqueryBuilderImpl(MainQuery mainQuery, T result, SetOp
super(mainQuery, result, operator, nested, listener, initiator);
}

@Override
protected void applyImplicitJoins() {
// There is nothing to do here for final builders as they don't have any nodes
}

@Override
public T end() {
subListener.verifySubqueryBuilderEnded();
Expand Down
Loading