The first name and last name arguments are checked. If the argument is not null, a
·
new Predicate will be constructed using the CriteriaBuilder() method (i.e., the
CriteriaBuilder.and() method). The method equal() is to specify an equal
restriction, within which the Root.get() was called, passing in the corresponding
attribute of the entity class's metamodel to which the restriction applies. The
constructed predicate was then "conjunct" with the existing predicate (stored by
the variable criteria) by calling the CriteriaBuilder.and() method.
The Predicate is constructed with all the criteria and restrictions and passed as
·
the where clause to the query by calling the CriteriaQuery.where() method.
Finally, CriteriaQuery was passed to the EntityManager. The EntityManager will
·
then construct the query based on the CriteriaQuery passed in, execute the query,
and return the result.
To test the criteria query operation, add the code snippet in Listing 10-29 to the main() method of
the JpaSample class.
Listing 10-29. Testing the findByCriteriaQuery() Method
// Find contact by criteria query
contacts = contactService.findByCriteriaQuery("John", "Smith");
listContactsWithDetail(contacts);
Running the program will produce the following output (other output was omitted):
2011-10-20 18:42:34,411 INFO [com.apress.prospring3.ch10.service.jpa.ContactServiceImpl] -
<Finding contact for firstName: John and lastName: Smith>
...
Listing contacts with details:
Contact - Id: 3, First name: John, Last name: Smith, Birthday: 1964-02-28
You can try a different combination or pass null value to either of the arguments to observe the
output.
Introducing Spring Data JPA
The Spring Data JPA project is a subproject under the Spring Data umbrella project. The main objective
of the Spring Data JPA project is to provide additional features for simplifying application development
with JPA.
Spring Data JPA provides several main features. In this section, we will discuss two main features.
The first one is the Repository abstraction, while the other one is the entity listener for keeping track of
basic audit information of entity classes.
Adding Spring Data JPA Library Dependencies
To use Spring Data JPA, we need to add several dependencies to the project. Table 10-2 describes the
Maven dependencies required.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home