DATAJPA-763 - Building up joins now considers already registered fetches, too.

QueryUtils.toExpressionRecursively(…) now also checks the provided root for already existing fetches to avoid creating superfluous join.

Original pull request: #151.
This commit is contained in:
Matthias Herrmann
2015-07-20 08:45:07 +02:00
committed by Oliver Gierke
parent 0cbfefc722
commit 77ce120e46
2 changed files with 32 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
@@ -39,7 +40,9 @@ import javax.persistence.spi.PersistenceProviderResolverHolder;
import org.hibernate.ejb.HibernatePersistence;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.jpa.domain.sample.Category;
import org.springframework.data.jpa.domain.sample.Order;
import org.springframework.data.jpa.domain.sample.Product;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.test.context.ContextConfiguration;
@@ -142,6 +145,21 @@ public class QueryUtilsIntegrationTests {
}
}
/**
* @see DATAJPA-763
*/
@Test
public void doesNotCreateAJoinForAlreadyFetchedAssociation() {
final CriteriaBuilder builder = em.getCriteriaBuilder();
final CriteriaQuery<Category> query = builder.createQuery(Category.class);
final Root<Category> root = query.from(Category.class);
root.fetch("product", JoinType.LEFT);
QueryUtils.toExpressionRecursively(root, PropertyPath.from("product", Category.class));
assertThat(root.getJoins(), is(empty()));
}
protected void assertNoJoinRequestedForOptionalAssociation(Root<Order> root) {
assertThat(root.getJoins(), is(empty()));
}