How do you set up a request with an uncertain number of parameters (I don't know how to speak more clearly) at JPA?



  • There general is a method that translates data from OBD using filters. There are some fields on the uniform, and depending on them the different results of the request.

    public List<PollEvent> getReport(String subscriberMSISDN, String operatorID, String operatorNickname,
                                         Date fromDate, Date toDate, String msg1, String msg2,
                                         int first, int pageSize) {
            List<PollEvent> result = new ArrayList<>();
            try {
                        result = em.createQuery("from PollEvent c WHERE c.call.operators.operator_avaya_id = :operator_avaya_id", PollEvent.class)
                        .setParameter("operator_avaya_id", operator_avaya_id )
                        .setFirstResult(first)
                        .setMaxResults(pageSize)
                        .getResultList();
            } catch (Exception e) {
                this.logger.log(Level.SEVERE, null, e);
            }
            return result;
        }
    

    The problem is, the user can choose different filths and I need to make a request on them. I don't know how to do it at JPA, but they're all curves for me or I just don't understand how horses work.

    I found one solution for myself, but I think he's a crutch. That's what you write every parameter. if and check it out. null And it's gonna be like this if it doesn't. null It's just that you put another condition on the line, "AND field = :field" and add .setParameter("field ", field)

    But again, I think it's a crutch.

    I also found solutions through.

    public List<T> findFromNamedQueryWithParams(String namedQuery, Map<String, Object> params) {
            TypedQuery<T> query = getEntityManager().createNamedQuery(namedQuery, entityClass);
            if (params != null) {
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    query.setParameter(entry.getKey(), entry.getValue());
                }
            }
            return query.getResultList();
        }
    

    But there's an ambush too. You have to make a pile. NamedQuery In my classroom, it's crazy, or it's something else to do. If anyone would say thank you.

    I also found an option. CriteriaBuilder https://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/Criteria But I didn't get it again.



  • You've got to build a request that matches the filter using JPQL or CriteriaBuilder, which is easier for you.
    Somehow, the construction of a request for the CriteriaBuilder-

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery();
        Root e = cq.from(Entity.class);
        Predicate where = cb.conjunction(); // Надо задать какое-то начальное значение. Пустая конъюнкция годится практически в любом случае
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            if (entry.getKey().equals("field1")) {
                where = cb.and(where, cb.equal(e.get("field1"), cb.parameter(здесь_класс_параметра_field1, "field1")));
            }
            if (entry.getKey().equals("field2")) {
                where = cb.and(where, cb.equal(e.get("field2"), cb.parameter(здесь_класс_параметра_field2, "field2")));
            }
            ...
        }
        ...
        Query q = em.createQuery(cq.where(where));
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            q.setParameter(entry.getKey(), entry.getValue());
        }
        return q.getResultList();
    

    Conditions WHERE dynamically formed in a large for

    JPQL isn't really much easier. Mainly because there is no easy function to build WHERE♪ Either you cook them yourself or they'll be too loud without them.
    In a case where circumstances are good, for can be replaced by a short as for the installation of parameters. This is when the field of the entity and the keys of the filter coincide and the parameter class can be extracted from, for example, the value of the filter.

    So, if it doesn't scare you, study the CriteriaBuilder.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2