重用多个存储库标准的最佳方法是什么?

时间:2015-02-23 16:45:50

标签: java jpa repository criteria

我有一个存储库层,它有许多方法组合,以匹配搜索标准。重用此标准的最佳方法是什么?我认为像findByNameAndIdAndBirthdayAndAccounaNumber这样的方法名称不是个好主意!谢谢!

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);

}

2 个答案:

答案 0 :(得分:2)

听起来您可能正在寻找规范模式,它允许您编写如下方法:

public Order findOrderBySpecification(Specification specification) {

}

然后使用一个或多个规范的组合来调用它,例如按帐号,帐号和姓名等。

这里有一个使用Criteria API的例子:

http://java.dzone.com/articles/java-using-specification

另请参阅下面的文章,该文章引用了Spring Data项目,但即使您不使用Spring,也可能值得一读。

http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

此外,您可以使用上面文章中引用的QueryDSL库而不是使用Straight JPA(即没有Spring)的相当冗长的Criteria API来更简单地实现此目的。

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

答案 1 :(得分:1)

尝试在存储库实现中使用规范模式。

OrderSpecificationByName.java

public class OrderSpecificationByName implements OrderSpecification, HibernateSpecification {
    private String name;

    public OrderSpecificationByName(String name) {
        super();
        this.name = name;
    }

    @Override
    public boolean isSatisfiedBy(Object order) {
        return ((Order)order).hasName(name);
    }

    @Override
    public Criterion toCriteria() {
        return Restrictions.eq("name", name);
    }
}

OrderSpecificationById.java

public class OrderSpecificationById implements OrderSpecification, HibernateSpecification { 
    private Long id;

    public OrderSpecificationById(String id) {
        super();
        this.id = id;
    }

    @Override
    public boolean isSatisfiedBy(Object order) {
        return ((Order)order).hasId(id);
    }

    @Override
    public Criterion toCriteria() {
        return Restrictions.eq("id", id);
    }
}

然后您必须遵守逻辑规范AndSpecificationOrSpecificationNotSpecification等。

AndSpecification.java

public class AndSpecification implements HibernateSpecification {
    private Specification first;
    private Specification second;

    public AndSpecification(Specification first, Specification second) {
        first = first;
        second = second;
    }

    @Override
    public boolean isSatisfiedBy(Object candidate) {
        return first.isSatisfiedBy(candidate) && second.isSatisfiedBy(candidate);
    }

     @Override
    public Criterion toCriteria() {
        Conjunction conjuntion = Restrictions.conjunction();
        conjuntion.add(first.toCriteria());
        conjuntion.add(second.toCriteria());

        return conjuntion;
    }
}

OrderRepository.java

public List<Order> query(HibernateSpecification specification) {    
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Order.class);
    criteria(specification.toCriteria());
    return criteria.list(); 
}