DAO Implementation
A generic DAO interface
Dao.java
package dao;
import java.util.List;
import entity.AbstractEntity;
public interface Dao<T extends AbstractEntity, I> {
List<T> findAll();
T find(I id);
T save(T newEntry);
void delete(I id);
}
The JpaSpecificationExecutor
In other words, if we need to modify our repository interface to support database queries that use the JPA Criteria API, we have to follow these steps:
- Extend the JpaSpecificationExecutor
interface. - Set the type of the managed entity.
On top of the JpaSpecificationExecutor there is a PagingAndSortingRepository abstraction that adds additional methods to ease paginated access to entities.
ExpenseDao.java
package dao;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import entity.Expense;
public interface ExpenseDao extends PagingAndSortingRepository<Expense, Long>, JpaSpecificationExecutor<Expense> {
}