There are two ways that you can accomplish this using Java 7. The first way is to create a generic base repository that will apply the custom bindings to all of the implemented model repositories. For example:
public class GenericModelRepository<T, ID extends Serializable, S extends EntityPath<T>> extends QueryDslJpaRepository<T, ID> implements QuerydslBinderCustomizer<S> {
public GenericModelRepository(
JpaEntityInformation<T, ID> entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
}
public GenericModelRepository(
JpaEntityInformation<T, ID> entityInformation,
EntityManager entityManager,
EntityPathResolver resolver) {
super(entityInformation, entityManager, resolver);
}
@Override
public void customize(QuerydslBindings bindings, S t) {
bindings.bind(String.class).first(new SingleValueBinding<StringPath, String>() {
@Override
public Predicate bind(StringPath path, String s) {
return path.equalsIgnoreCase(s);
}
});
}
}
To tell Spring Data to use this base repository when implementing all of your custom repository interfaces, simply add it as the repositoryBaseClass
in the @EnableJpaRepositories
annotation:
@Configuration
@EnableJpaRepositories(basePackages = { "me.woemler.project.repositories" }, repositoryBaseClass = GenericModelRepository.class)
@EnableTransactionManagement
public class RepositoryConfig { ... }
@RepositoryRestResource
public interface PersonRepository extends JpaRepository<Person, Long>,
QueryDslPredicateExecutor<Person>,
QuerydslBinderCustomizer<EntityPath<Person>> {
}
Now all web service StringPath
query operations will be case-insensitive equality tests:
GET http://localhost:8080/persons?name=joe%20smith
"_embedded": {
"persons": [
{
"name": "Joe Smith",
"gender": "M",
"age": 35,
"_links": {
"self": {
"href": "http://localhost:8080/persons/1"
},
"person": {
"href": "http://localhost:8080/persons/1"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/persons"
},
"profile": {
"href": "http://localhost:8080/profile/persons"
}
},
"page": {
"size": 20,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
The second option, should you want more fine control over how each repository handling its bindings would be to create an Impl
version of the repositories you wish to customize:
public class PersonRepositoryImpl implements QuerydslBinderCustomizer<EntityPath<Person>> {
@Override
public void customize(QuerydslBindings bindings, EntityPath<Person> t) {
bindings.bind(String.class).first(new SingleValueBinding<StringPath, String>() {
@Override
public Predicate bind(StringPath path, String s) {
return path.equalsIgnoreCase(s);
}
});
}
}
You can then use the @EnableJpaRepositories
annotation normally, but you must create an Impl
instance for each of your repository interfaces that you wish to customize.