Spring Data JPA is a wonderful project that generate DAOs for you, and more! You only have to create an interface (without any implementation):
interface PaymentMethodsDao extends JpaRepository<PaymentMethods, Integer> {}
This interface (via inherited JpaRepository
) will automatically give you:
PaymentMethod save(PaymentMethod entity);
Iterable<PaymentMethod> save(Iterable<? extends PaymentMethod> entities);
PaymentMethod findOne(Integer id);
boolean exists(Integer id);
Iterable<PaymentMethod> findAll();
long count();
void delete(Integer id);
void delete(PaymentMethod entity);
void delete(Iterable<? extends PaymentMethod> entities);
void deleteAll();
Iterable<PaymentMethod> findAll(Sort sort);
Page<PaymentMethod> findAll(Pageable pageable);
List<PaymentMethod> findAll();
List<PaymentMethod> findAll(Sort sort);
List<PaymentMethod> save(Iterable<? extends PaymentMethods> entities);
void flush();
PaymentMethod saveAndFlush(PaymentMethods entity);
void deleteInBatch(Iterable<PaymentMethods> entities);
The interface is strongly typed (generics) and automatically implemented for you. For every entity all you have to do is to create an interface extending JpaRepository<T,Integer extends Serializable>
.
But wait, there's more! Assuming your PaymentMethod
has name
and validSince
persistent fields. If you add the following method to your interface:
interface PaymentMethodsDao extends JpaRepository<PaymentMethods, Integer> {
Page<PaymentMethod> findByNameLikeAndValidSinceGreaterThan(
String name, Date validSince, Pageable page
);
}
the framework will parse the method name:
findBy
(Name like) And
(ValidSince greater than)
create the JPA QL query, apply paging and sorting (Pageable page
) and run it for you. No implementation needed:
paymentMethodsDao.findByNameLikeAndValidSinceGreaterThan(
"abc%",
new Date(),
new PageRequest(0, 20, Sort.Direction.DESC, "name"
);
Resulting query:
SELECT * //or COUNT, framework also returns the total number of records
FROM PaymentMethods
WHERE name LIKE "abc%"
AND validSince > ...
And with paging applied.
The only downside is that the project is rather new and it is relatively easy to hit buts (but it is very actively developed).