DATAJPA-292 - Manually defined queries can contain like expressions now.
This commit introduces support for JPQL inline like expressions such as "… like ?1%". The implementation will detect those expressions and turn the query back into a valid JPQL one. On query execution the parameters will be bound according to the like expression defined in the query.
This commit is contained in:
@@ -999,6 +999,34 @@ public class UserRepositoryTests {
|
||||
assertThat(page, hasItem(firstUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-292
|
||||
*/
|
||||
@Test
|
||||
public void executesManualQueryWithPositionLikeExpressionCorrectly() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
List<User> result = repository.findByFirstnameLike("Da");
|
||||
|
||||
assertThat(result, hasSize(1));
|
||||
assertThat(result, hasItem(thirdUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-292
|
||||
*/
|
||||
@Test
|
||||
public void executesManualQueryWithNamedLikeExpressionCorrectly() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
List<User> result = repository.findByFirstnameLikeNamed("Da");
|
||||
|
||||
assertThat(result, hasSize(1));
|
||||
assertThat(result, hasItem(thirdUser));
|
||||
}
|
||||
|
||||
private Page<User> executeSpecWithSort(Sort sort) {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.jpa.repository.query.StringQuery.LikeBinding;
|
||||
import org.springframework.data.repository.query.parser.Part.Type;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class LikeBindingUnitTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullName() {
|
||||
new LikeBinding(null, Type.CONTAINING);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsEmptyName() {
|
||||
new LikeBinding("", Type.CONTAINING);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullType() {
|
||||
new LikeBinding("foo", null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsInvalidType() {
|
||||
new LikeBinding("foo", Type.SIMPLE_PROPERTY);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsInvalidPosition() {
|
||||
new LikeBinding(0, Type.CONTAINING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsUpInstanceForName() {
|
||||
|
||||
LikeBinding binding = new LikeBinding("foo", Type.CONTAINING);
|
||||
|
||||
assertThat(binding.hasName("foo"), is(true));
|
||||
assertThat(binding.hasName("bar"), is(false));
|
||||
assertThat(binding.hasName(null), is(false));
|
||||
assertThat(binding.hasPosition(0), is(false));
|
||||
assertThat(binding.getType(), is(Type.CONTAINING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsUpInstanceForIndex() {
|
||||
|
||||
LikeBinding binding = new LikeBinding(1, Type.CONTAINING);
|
||||
|
||||
assertThat(binding.hasName("foo"), is(false));
|
||||
assertThat(binding.hasName(null), is(false));
|
||||
assertThat(binding.hasPosition(0), is(false));
|
||||
assertThat(binding.hasPosition(1), is(true));
|
||||
assertThat(binding.getType(), is(Type.CONTAINING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void augmentsValueCorrectly() {
|
||||
|
||||
assertAugmentedValue(Type.CONTAINING, "%value%");
|
||||
assertAugmentedValue(Type.ENDING_WITH, "%value");
|
||||
assertAugmentedValue(Type.STARTING_WITH, "value%");
|
||||
|
||||
assertThat(new LikeBinding(1, Type.CONTAINING).prepare(null), is(nullValue()));
|
||||
}
|
||||
|
||||
private static void assertAugmentedValue(Type type, Object value) {
|
||||
|
||||
LikeBinding binding = new LikeBinding("foo", type);
|
||||
assertThat(binding.prepare("value"), is(value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.jpa.repository.query.StringQuery.LikeBinding;
|
||||
import org.springframework.data.repository.query.parser.Part.Type;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class StringQueryUnitTests {
|
||||
|
||||
@Test
|
||||
public void detectsPositionalLikeBindings() {
|
||||
|
||||
StringQuery query = new StringQuery("select u from User u where u.firstname like %?1% or u.lastname like %?2");
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(),
|
||||
is("select u from User u where u.firstname like ?1 or u.lastname like ?2"));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(2));
|
||||
|
||||
LikeBinding binding = bindings.get(0);
|
||||
assertThat(binding, is(notNullValue()));
|
||||
assertThat(binding.hasPosition(1), is(true));
|
||||
assertThat(binding.getType(), is(Type.CONTAINING));
|
||||
|
||||
binding = bindings.get(1);
|
||||
assertThat(binding, is(notNullValue()));
|
||||
assertThat(binding.hasPosition(2), is(true));
|
||||
assertThat(binding.getType(), is(Type.ENDING_WITH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectsNamedLikeBindings() {
|
||||
|
||||
StringQuery query = new StringQuery("select u from User u where u.firstname like %:firstname");
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(), is("select u from User u where u.firstname like :firstname"));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(1));
|
||||
|
||||
LikeBinding binding = bindings.get(0);
|
||||
assertThat(binding, is(notNullValue()));
|
||||
assertThat(binding.hasName("firstname"), is(true));
|
||||
assertThat(binding.getType(), is(Type.ENDING_WITH));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsDifferentBindingsForRepeatedParameter() {
|
||||
new StringQuery("select u from User u where u.firstname like %?1 and u.lastname like ?1%");
|
||||
}
|
||||
}
|
||||
@@ -124,6 +124,18 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
|
||||
|
||||
List<User> findByFirstnameNotIn(Collection<String> firstnames);
|
||||
|
||||
/**
|
||||
* @see DATAJPA-292
|
||||
*/
|
||||
@Query("select u from User u where u.firstname like ?1%")
|
||||
List<User> findByFirstnameLike(String firstname);
|
||||
|
||||
/**
|
||||
* @see DATAJPA-292
|
||||
*/
|
||||
@Query("select u from User u where u.firstname like :firstname%")
|
||||
List<User> findByFirstnameLikeNamed(@Param("firstname") String firstname);
|
||||
|
||||
/**
|
||||
* Manipulating query to set all {@link User}'s names to the given one.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user