I have a Spring Data Rest repository which utilises the QueryDSL support outlined here:
https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#spring-data-rest
The default is to query all specified parameters using equals. A mechanism to override the param binding to something other than equals is given in the same article however it requires Java 8.
https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#querydsl-web-support
Is there any clean way in Java 7 to achieve the same functionality?
Update
I can get the binding customization working as below:
@Override
public void customize(QuerydslBindings bindings, QMember member) {
bindings.bind(member.forename).first(new SingleValueBinding<StringPath, String>() {
@Override
public Predicate bind(StringPath path, String value) {
return path.like(value);
}
});
bindings.bind(member.surname).first(new SingleValueBinding<StringPath, String>() {
@Override
public Predicate bind(StringPath path, String value) {
return path.startsWith(value);
}
});
}
However the examples all use a Java 8 default method on the repository interface to have them applied.
public interface MemberRepository extends JpaRepository<Member, Long>,
QueryDslPredicateExecutor<Member>,QuerydslBinderCustomizer<QMember> {
default void customize(QuerydslBindings bindings, QMember member) {
....
}
}
In Java 7 this is obviously not possible. I have tried with a custom repository however that fails with an error:
org.springframework.data.mapping.PropertyReferenceException: No property customize found for type Member!
public interface MemberRepositoryCustom {
public void customize(QuerydslBindings bindings, QMember member);
}
public class MemberRepositoryCustomImpl implements MemberRepositoryCustom {
@Override
public void customize(QuerydslBindings bindings, QMember member) {
bindings.bind(member.forename).first(new SingleValueBinding<StringPath, String>() {
@Override
public Predicate bind(StringPath path, String value) {
return path.like(value);
}
});
bindings.bind(member.surname).first(new SingleValueBinding<StringPath, String>() {
@Override
public Predicate bind(StringPath path, String value) {
return path.startsWith(value);
}
});
}
}
See Question&Answers more detail:os