A SQL resultset mapping called contactResult was defined for the entity class, with the entityClass
attribute in the Contact class itself. JPA supports more complex mapping for multiple entities and
supports mapping down to column-level mapping.
After the SQL resultset mapping is defined, the findAllByNativeQuery() method can be invoked
using the resultset mapping's name. Listing 10-25 shows the code snippet.
Listing 10-25. Implementing the findAllByNativeQuery() Method Using SQL Resultset Mapping
package com.apress.prospring3.ch10.service.jpa;
// Other code omitted
public class ContactServiceImpl implements ContactService {
// Other code omitted
@Transactional(readOnly=true)
public List<Contact> findAllByNativeQuery() {
return em.createNativeQuery(ALL_CONTACT_NATIVE_QUERY,
"contactResult").getResultList();
}
}
As you can see, JPA also provides strong support for executing native queries, with a flexible SQL
resultset mapping facility provided.
Criteria Query Using the JPA 2 Criteria API
Most applications will provide a frontend for users to search for information. Most likely a large number of
searchable fields will be displayed, and the users will enter only some of them and do the search. It's very
difficult to prepare a large number of queries with each possible combination of the parameters that users
may choose to enter. In this situation, the criteria API query feature comes to the rescue.
In JPA 2, one major new feature introduced was a strongly typed criteria API query. In this new
criteria API, the criteria being passed into the query is based on the mapped entity classes' metamodel.
As a result, each criteria specified is strongly typed, and errors will be discovered at compile time, rather
than runtime.
In the JPA criteria API, an entity class's metamodel is represented by the entity class name with a
suffix of an underscore (_). For example, the metamodel class for the Contact entity class will be
Contact_. Listing 10-26 shows the Contact_ class.
Listing 10-26. JPA 2 Strongly Typed Criteria API: Metamodel
package com.apress.prospring3.ch10.domain;
import
java.util.Date;
import
javax.persistence.metamodel.SetAttribute;
import
javax.persistence.metamodel.SingularAttribute;
import
javax.persistence.metamodel.StaticMetamodel;
@StaticMetamodel(Contact.class)
public abstract class Contact_ {
public static volatile SingularAttribute<Contact, Long> id;
public static volatile SetAttribute<Contact, ContactTelDetail>
contactTelDetails;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home