简体   繁体   English

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

[英]What is the best approach to reuse multiple repository criterias?

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 ! 我认为像findByNameAndIdAndBirthdayAndAccounaNumber这样的方法名称不是一个好主意! 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: 听起来您可能正在寻找“规范”模式,这将使您可以编写如下方法:

public Order findOrderBySpecification(Specification specification) {

}

and then call this using a combination of one or more specifications eg by account number, by account number and name etc. 然后结合使用一种或多种规范(例如,按帐号,按帐号和名称等)进行调用。

There is an example here using the Criteria API: 这里有一个使用Criteria API的示例:

http://java.dzone.com/articles/java-using-specification 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. 另请参见下面的文章,该文章引用了Spring Data项目,但是即使您没有使用Spring,也仍然值得一读。

http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ 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 (ie without Spring). 另外,您可以使用上一篇文章中引用的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 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 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 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);
    }
}

Then you must implment the logical specifications AndSpecification , OrSpecification , NotSpecification , etc.. 然后,您必须实施逻辑规范AndSpecificationOrSpecificationNotSpecification等。

AndSpecification.java 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 OrderRepository.java

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用相似对象的多个服务中重用相似方法的最佳方法是什么? - What is the best approach to reuse similar method in multiple Services that use similar objects? 封装多个方法调用的最佳方法是什么? - What is the best approach to encapsulate multiple method calls? 通过多个线程从数据库加载数据的最佳方法是什么 - What is the best approach for loading data from DB by multiple threads 在多个微服务之间共享实用程序 class 的最佳方法是什么? - What is best approach to share a utility class between multiple microservices? 使用 Java 创建多个同名 xml 元素的最佳方法是什么? - What is the best approach to create multiple xml elements with same name with Java? 在资源控制器中使用多个服务的最佳方法是什么? - What is the best approach to use multiple services inside a resource controller? 覆盖多个 SeleniumJupiter 配置参数的最佳方法是什么? - What would be the best approach to overwrite multiple SeleniumJupiter configuration parameters? 初始化对象的最佳方法是什么 - What is the best approach for initialize object 加密加密的最佳方法是什么? - What is best approach to encrypting an encryption? 使用JasperReports的最佳方法是什么? - What is the best approach to use JasperReports?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM