Skip to content

Commit

Permalink
Fixed #190 - Implemented fetchOnly() as a way to specify the fetch ba…
Browse files Browse the repository at this point in the history
…se for relations that should be fetched. Fixed date extract test.
  • Loading branch information
beikov authored and Mobe91 committed Mar 18, 2017
1 parent 0b5e36f commit dbea7da
Show file tree
Hide file tree
Showing 26 changed files with 548 additions and 95 deletions.
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;
}

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

0 comments on commit dbea7da

Please sign in to comment.