I have an repository layer that have many methods combination, to match search criterias.. What is the best approach to reuse this criterias? I think that methods name like findByNameAndIdAndBirthdayAndAccounaNumber is not a good idea ! Thanks !
public Order findByIdAndName(String orderId) {
List<OrderEntity> list = entityManager.createNamedQuery(OrderEntity.QUERY_FIND_BY_ORDERID_AND_NAME, OrderEntity.class)
.setParameter("orderId", orderId)
.setParameter("name", name).getResultList();
if (list.isEmpty()) {
return null;
}
OrderEntity orderEntity = list.get(0);
return toOrder(orderEntity);
}
Sounds like you may be looking for the Specification pattern which would allow you write a method like:
and then call this using a combination of one or more specifications e.g. by account number, by account number and name etc.
There is an example here using the Criteria API:
http://java.dzone.com/articles/java-using-specification
See also the article below which refers to the Spring Data project but which is probably worth a read anyway even if you are not using Spring.
http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
Also, you can achieve this more simply using the QueryDSL library referenced in the article above rather than the rather verbose Criteria API with Straight JPA (i.e. without Spring).
http://blog.mysema.com/2010/04/querydsl-as-alternative-to-jpa-2.html http://www.querydsl.com/static/querydsl/2.1.0/reference/html/ch02s02.html
Try to use the specification pattern in your repository implementation.
OrderSpecificationByName.java
OrderSpecificationById.java
Then you must implment the logical specifications
AndSpecification
,OrSpecification
,NotSpecification
, etc..AndSpecification.java
OrderRepository.java