public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
T save(T entity);
Iterable<T> save(Iterable<? extends T> entities);
T findOne(ID id);
boolean exists(ID id);
Iterable<T> findAll();
long count();
void delete(ID id);
void delete(T entity);
void delete(Iterable<? extends T> entities);
void deleteAll();
}
The method name is quite self-explanatory. It's better to show how the Repository abstraction
works by going through a simple example. Let's revise the ContactService interface a bit, down to just
three finder methods. Listing 10-31 shows the revised ContactService interface. After the ContactService
interface is revised, the com.apress.prospring3.ch10.JpaSample and
com.apress.prospring3.ch10.service.jpa.ContactServiceImpl classes will have errors. Just delete these
two classes and proceed with the implementation of the interface using Spring Data JPA.
Listing 10-31. The Revised ContactService Interface
package com.apress.prospring3.ch10.service;
import java.util.List;
import com.apress.prospring3.ch10.domain.Contact;
public interface ContactService {
// Find all contacts
public List<Contact> findAll();
// Find contacts by first name
public List<Contact> findByFirstName(String firstName);
// Find contacts by first name and last name
public List<Contact> findByFirstNameAndLastName(
String firstName, String lastName);
}
The next step is to prepare the ContactRepository interface, which extends the CrudRepository
interface. Listing 10-32 shows the ContactRepository interface.
i
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home