I'm used to .Net and LINQtoEntities, especially the IQueryable part which allows to carry a request through different functions before fetching the results.
Does anything like this exist in spring data ? Or any other java ORM ?
Basic example of what I'd like to be able to do :
private IQueryable<Todo> GetAll(){
context.Todos.Where(t => !t.Deleted);
}
public IEnumerable<Todo> GetDoneTodos(){
GetAll().Where(t => t.Done).ToList();
}
You can use Spring Data's QueryDSL integration. Basically, you extend the
QueryDslPredicateExecutor
in your repository interface and it add afindAll
method that gets a QueryDSLPredicate
and filter all the results based on thatPredicate
. Suppose we have domain object, sayGreeting
, then we'd have repository like this:Then you can use generated
QModel
s generated by QueryDSL to create aPredicate
and pass it to ourgreetingRepository
. Suppose we're going to filter all theGreeting
s by one specific user:greeting
is a metamodel generated by QueryDSL based on ourGreeting
model.Note 1: You can see how you can integrate Spring Data and QueryDSL here and see more examples for QueryDSL's
Predicate
s here.Note 2:
QueryDslPredicateExecutor
also providesfindOne(Predicate predicate)
,count(Predicate predicate)
andexists(Predicate predicate)
methods which are useful and self-explanatory, of course.Note 3: You can achieve almost the same thing with
Specification
s but in my opnion QueryDSL has more elegant and readablePredicate
s.