listContacts(contacts);
}
private static void listContacts(List<Contact> contacts) {
System.out.println("");
System.out.println("Listing contacts without details:");
for (Contact contact: contacts) {
System.out.println(contact);
System.out.println();
}
}
}
There's nothing special about the class. Running the program will yield the contact listing as
expected. We leave it to you for testing it.
You have seen how Spring Data JPA can help simplify the development. We don't need to prepare a
named query, don't need to call the EntityManager.createQuery() method, and so on.
What we covered were only the simplest examples. The Repository abstraction supports a lot of
features, including defining custom queries with the @Query annotation, the Specification feature
(belong to the Spring Data JPA project as of version 1.0.1.RELEASE), and so on. For more information,
please refer to SpringSource's reference documentation listed here:
Spring Data Commons Project: http://static.springsource.org/
·
spring-data/data-commons/docs/current/reference/html/
Spring Data JPA Project: http://static.springsource.org/spring-data/
·
data-jpa/docs/current/reference/html/
Keeping Track of Changes on the Entity Class
In most applications, we need to keep track of basic audit activities for the business data being
maintained by users. The audit information typically includes the user who creates the data, the date it
was created, the date it was last modified, and the user who last modified it.
The Spring Data JPA provides a function in the form of a JPA entity listener, which helps you keep
track of those audit information automatically. To use the feature, the entity class needs to implement
the org.springframework.data.domain.Auditable<U,ID extends Serializable> interface (belonging to
Spring Data Commons) or extend any class that implemented the interface. Listing 10-36 shows the
Auditable interface that was extracted from Spring Data's reference documentation.
Listing 10-36. The Auditable Interface
package org.springframework.data.domain;
import java.io.Serializable;
import org.joda.time.DateTime;
public interface Auditable<U, ID extends Serializable>
extends Persistable<ID> {
U getCreatedBy();
void setCreatedBy(final U createdBy);
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home