3/24/2011

Using the DynamicQuery API in Liferay with more than one group by

I love the Dynamic Query APi for Liferay ! It allows me to query everything I want from the liferay database without having to write a single line of sql.


And now I know how to specify more than one projection. Just use the ProjectionList !


If you want to group your results, for example, by firstName and lastName, you could do it like this:



ProjectionList projectionList = ProjectionFactoryUtil.projectionList();

projectionList.add(ProjectionFactoryUtil.groupProperty("firstName"));
projectionList.add(ProjectionFactoryUtil.groupProperty("lastName"));
myQuery.setProjection(projectionList);



It also allows you to group by one column and return the count of another:



ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
projectionList.add(ProjectionFactoryUtil.groupProperty("firstName"));
projectionList.add(ProjectionFactoryUtil.count("lastName"));
myQuery.setProjection(projectionList);



You can also specify aliases (just look at the ProjectionList outline) ... all in all a very powerful feature which I, from now on will use a little more often!




If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.