Simple Java project demonstrating how to use Lambda expressions with JPA Criteria API.
Switch branches/tags
Nothing to show
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/main Initial version Jan 30, 2016
.gitignore Initial version Jan 30, 2016
LICENSE Initial commit Jan 30, 2016 Initial version of the README file Jan 30, 2016
pom.xml Initial version Jan 30, 2016


Simple Java project demonstrating how to use Lambda expressions with JPA Criteria API.

The project shows how to write Java Persistence API (JPA) Criteria queries in an easier way using Lambda expressions added to Java in version 8. You can take its code and use it in your own projects.


I was thinking about how to make Criteria API code easier. The problem is that you usually need several objects to build a query and the calls cannot be streamlined. it’s possible to write a callback interface with a method getting all the necessary parameters to build a query, but that’s just making a new API on top of the existing one without it being significantly easier. Then I realized that Java 8 comes with Lambda expression support. It allows us to write basic functions without a need to wrap them in classes. Lambda expressions can be used as method parameters where an object implementing a single method interface is expected.


Just download the project source files, and build it with Maven

mvn package

The project contains a main class, so it's executable. The main class creates an in-memory H2 database, inserts some records and then calls queries written as Lambda expressions.

java -jar target/JpaCriteriaWithLambdaExpressions-1.0-SNAPSHOT.jar

The most interesting class of the project is the AbstractDao class and its usage in the CarDao class.


These are some examples of using Lambda expressions for building criteria queries using a DAO support method:

findWhere(Car.class, (cb, root, query)
   -> (query.where(cb.equal(root.get(Car_.colour), colour))
createQuery(Car.class, (cb, root) -> (cb.equal(root.get(Car_.colour), colour)))