DATAJPA-403 - Improve predicate building by reusing existing joins if possible.
QueryUtils now checks already existing joins and reuses them when building expressions. Inspired by the pull request #41 by Alexandre Payment but polished and added integration test to make sure the joins really get reused.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link QueryUtils}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:infrastructure.xml")
|
||||
public class QueryUtilsIntegrationTests {
|
||||
|
||||
@PersistenceContext EntityManager em;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-403
|
||||
*/
|
||||
@Test
|
||||
public void reusesExistingJoinForExpression() {
|
||||
|
||||
CriteriaBuilder builder = em.getCriteriaBuilder();
|
||||
CriteriaQuery<User> query = builder.createQuery(User.class);
|
||||
Root<User> from = query.from(User.class);
|
||||
|
||||
PropertyPath managerFirstname = PropertyPath.from("manager.firstname", User.class);
|
||||
PropertyPath managerLastname = PropertyPath.from("manager.lastname", User.class);
|
||||
|
||||
QueryUtils.toExpressionRecursively(from, managerLastname);
|
||||
QueryUtils.toExpressionRecursively(from, managerFirstname);
|
||||
|
||||
assertThat(from.getJoins(), hasSize(1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user