To use the querydsl-collections module with generated query types, add an appropriate APT configuration like in the querydsl-hql or querydsl-jdoql setup setup.
Add the following static import :
import static com.mysema.query.collections.MiniApi.*; import static com.mysema.query.alias.Alias.*; // for alias usage
List<Cat> cats = ...; // some value QCat cat = new QCat("cat"); for (Cat cat : select(cats, cat.kittens.size().gt(0))){ System.out.println(cat.getName()); }
And here are some examples on using the full API
from(cat,cats).iterate(cat.name); from(cat,cats).iterate(cat.kittens); from(cat,cats).where(cat.kittens.size().gt(0)).iterate(cat.name); from(cat,cats).where(cat.name.eq("Kitty")).iterate(cat.name); from(cat,cats).where(cat.name.like("Kitt%")).iterate(cat.name);
If you use the MiniApi without available expressions you can use the factory methods of the MiniApi, which are accessible via the dollar-method, e.g.
for (String s : from($("str"), "a","ab","cd","de").where($("str").startsWith("a")).iterate($("str"))){ System.out.println(s); }
which prints out
a ab
For multiple variables the provided argument identifies the created path.
And here an example with a created integer path :
for (Integer i : from($(0),1,2,3,4).where($(0).lt(4)).iterate($(0))){ System.out.println(i); }
which prints out
1 2 3
And last but not least mixed arguments
for (Object o : from(1,2,"abc",5,3).where($().ne("abc")).iterate($())){ System.out.println(o); } {
which prints out
1 2 5 3
The alias usage builds on the factory methods for Expressions and extends them to support property access. Here are some examples.
At first an example query with APT generated domain types :
QCat cat = new QCat("cat"); for (String name : from(cat,cats) .where(cat.kittens.size().gt(0)) .iterate(cat.name)){ System.out.println(name); }
And now with an alias instance for the Cat class. The call "c.getKittens()" inside the dollar-method is internally transformed into the property path c.kittens.
Cat c = alias(Cat.class, "cat"); for (String name : from($(c),cats) .where($(c.getKittens()).size().gt(0)) .iterate($(c.getName()))){ System.out.println(name); }
The following example is a variation of the previous, where the access to the list size happens inside the dollar-method invocation.
Cat c = alias(Cat.class, "cat"); for (String name : from($(c),cats) .where($(c.getKittens().size()).gt(0)) .iterate($(c.getName()))){ System.out.println(name); }
All non-primitive and non-String typed properties of aliases are aliases themselves. So you may cascade method calls until you hit a primitive or String type in the dollar-method scope.
e.g.
$(c.getMate().getName())
is transformed into *c.mate.name* internally, but
$(c.getMate().getName().toLowerCase())
is not transformed properly, since the toLowerCase() invocation is not tracked.
Note also that you may only invoke getters, size(), contains(Object) and get(int) on alias types. All other invocations throw exceptions.