使用参数从JPQL查询中选择all

时间:2014-08-24 20:47:33

标签: java sql database jpa jpql

我在平面数据上有选择参数,只是不知道如何完全省略参数,或者使它成为一个完整的通配符。搜索可能使用一个或所有参数。这是怎么做到的?使用ANYALL?或者,还有另一种方式吗?

我想对所有参数使用一个通用查询,并为这些参数中的某些传入“all”或“any”,这些行。

现有代码:

package legacy.database;

import java.sql.Timestamp;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class MyQueries {

    private static final Logger log = Logger.getLogger(MyQueries.class.getName());
    private final EntityManagerFactory emf = Persistence.createEntityManagerFactory("LegacyDatabasePU");
    private final EntityManager em = emf.createEntityManager();

    public MyQueries() {
    }

    public List<Clients> findAll() {
        Query q = em.createQuery("select c from Clients c");
       List<Clients> clients = q.getResultList();
        return clients;
    }

    public List<Clients> selectWithParameters(Criteria c) {
        log.info(c.toString());
        String opener = c.getOpener();
        String closer1 = c.getCloser1();
        String status = c.getStatus();
        Query q = em.createQuery(
                "SELECT c FROM Clients c "
                + "WHERE c.status like :status "
                + "and c.closer1 like :closer1 "
                + "and c.opener like :opener");
        q.setParameter("opener", opener);
        q.setParameter("closer1", closer1);
        q.setParameter("status", status);
        log.info(q.toString());
        List<Clients> clients = q.getResultList();
        log.fine(clients.toString());
        return clients;
    }

    public Clients findById(int id) {
        Clients client = em.find(Clients.class, id);
        return client;
    }

    public void send(int id) {
        Clients c = em.find(Clients.class, id);
        java.util.Date date = new java.util.Date();
        Timestamp t = new Timestamp(date.getTime());
        em.getTransaction().begin();
        c.setDateUpdated(t.toString());
        em.getTransaction().commit();
    }
}

2 个答案:

答案 0 :(得分:2)

如果参数是可选的,则条件API提供了更多灵活性。 如果经常调用 selectWithParameters ,请考虑使用参数,因为DB可以缓存参数化查询。

带有可选参数的

selectWithParameters 如下所示:

public List<Clients> selectWithParameters(Criteria criteria) {
    log.info(criteria.toString());
    String opener = criteria.getOpener();
    String closer1 = criteria.getCloser1();
    String status = criteria.getStatus();

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Clients> query = criteriaBuilder.createQuery(Clients.class);
    Root<Clients> c = query.from(Clients.class);
    List<Predicate> wherePredicates = new LinkedList<Predicate>();
    if (null != status) {
        wherePredicates.add(criteriaBuilder.like(c.get("status"), status));
    }
    if (null != closer1) {
        wherePredicates.add(criteriaBuilder.like(c.get("closer1"), closer1));
    }
    if (null != opener) {
        wherePredicates.add(criteriaBuilder.like(c.get("opener"), opener));
    }
    query.where(wherePredicates.toArray(new Predicate[0]));

    List<Clients> clients = em.createQuery(query).getResultList();
    log.fine(clients.toString());
    return clients;
}

答案 1 :(得分:1)

谢谢海纳。这很有效,不知道为什么我遇到海纳的代码有问题,但他的样本让我朝着正确的方向前进:

public List<Clients> selectByCriteria(Criteria criteria) {
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Clients> clientCriteriaQuery = criteriaBuilder.createQuery(Clients.class);
    Root<Clients> clientRoot = clientCriteriaQuery.from(Clients.class);
    clientCriteriaQuery.select(clientRoot);
    List<Predicate> predicates = new ArrayList<>();
    predicates.add(criteriaBuilder.like(clientRoot.get(Clients_.phone1), "%" + criteria.getPhone1() + "%"));
    if (!criteria.getOpener().equalsIgnoreCase("all")) {
        predicates.add(criteriaBuilder.like(clientRoot.get(Clients_.opener), "%" + criteria.getOpener() + "%"));
    }
    if (!criteria.getCloser1().equalsIgnoreCase("all")) {
        predicates.add(criteriaBuilder.like(clientRoot.get(Clients_.closer1), "%" + criteria.getCloser1() + "%"));
    }
    if (!criteria.getStatus().equalsIgnoreCase("all")) {
        predicates.add(criteriaBuilder.like(clientRoot.get(Clients_.status), "%" + criteria.getStatus() + "%"));
    }
    clientCriteriaQuery.where(predicates.toArray(new Predicate[0]));
    List<Clients> clients = em.createQuery(clientCriteriaQuery).getResultList();
    return clients;
}

海纳的回答可能没有实质性差异(?)。 JPA和JPQL有点模糊。我无法相信,但我几乎更喜欢SQL!我必须调整。