DATACOUCH-241 - Document mixing SpEL and N1QL placeholders

This commit is contained in:
Simon Baslé
2016-08-18 12:55:05 +02:00
parent 7863688028
commit 415d8a2d23
3 changed files with 37 additions and 2 deletions

View File

@@ -13,4 +13,10 @@ public interface SpelRepository extends CrudRepository<User, String> {
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND username = \"#{oneCustomer}\"")
List<User> findCustomUsers();
//notice how the SpEL syntax #{[0]} considers the first method argument,
//and N1QL placeholder resolution will still consider every method argument as a placeholder value.
//thus N1QL placeholder used is $2, to match criteriaValue
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND #{[0]} = $2")
List<User> findUserWithDynamicCriteria(String criteriaField, Object criteriaValue);
}

View File

@@ -16,11 +16,14 @@
package org.springframework.data.couchbase.repository.spel;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.*;
import java.util.List;
import com.couchbase.client.java.Bucket;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -66,4 +69,15 @@ public class SpelRepositoryTests {
assertEquals("uname-3", users.get(0).getUsername());
}
@Test
public void testSpelArgumentResolution() {
List<User> usersByName = repository.findUserWithDynamicCriteria("username", "uname-5");
List<User> usersByAge = repository.findUserWithDynamicCriteria("age", 4);
assertThat(usersByName, hasSize(1));
assertThat(usersByAge, hasSize(1));
assertThat(usersByName.get(0).getKey(), is("testuser-5"));
assertThat(usersByAge.get(0).getKey(), is("testuser-4"));
}
}