1

I have a query that need to use "dynamic" parameters set. But coundn´t realize how to do it, i tried to not add the where string but it gives me erros because the parameter is seted.

Any tips?

String queryS = "select object(c) from "
                           + entityClassName + " as c " +
                            "where 1 = 1" ;

            if(codigoPaciente.compareTo("") != 0)
            {
                queryS += " and c.CodigoDoPaciente =:paciente";
            }
            if(codServicoPrincipal.compareTo("") != 0)
                queryS += " and c.codigoServicoPrincipal =:servico";
            if(data != null)
                queryS += " and c.codigoServicoPrincipal =:data";
            if(TipoServico.compareTo("") != 0)
                queryS += " and c.codigoServicoPrincipal =:tipoServico";


            Query query = em.createQuery(queryS);
            query.setParameter("paciente", codigoPaciente);
            query.setParameter("codigoServicoPrincipal", codServicoPrincipal);
            query.setParameter("data", data);
            query.setParameter("tipoServico", TipoServico);


            return query.getResultList();
0

2 Answers 2

2

Rather than doing a bunch of string concatenation to create a query, the criteria API could be used to construct the query in a more "safe" manner. Then you could add the condition and the value in a single conditional check.

Sign up to request clarification or add additional context in comments.

5 Comments

I agree with joel on this.
I only partially agree with myself. The criteria api is no joy to work with.
It may be a pain, but avoiding concatenating stuff usually pays off in the long run...
I was trying to avoid the criteriaAPI....but it seams that that is the only way to do that
See QueryDSL. Strongly typed dynamic queries without the pain of the criteria API. blog.mysema.com/2010/04/querydsl-as-alternative-to-jpa-2.html
0

You are not setting your parameters inside if conditions... Maybe set the parameter for each string you concatenate?

if(codigoPaciente.compareTo("") != 0)
            {
                queryS += " and c.CodigoDoPaciente =:paciente";
            }

Query query = em.createQuery(queryS);

// etc etc...
if(codigoPaciente.compareTo("") != 0)
    query.setParameter("paciente", codigoPaciente);

4 Comments

Another point; if you remove the WHERE clause and keep concatenating AND's without checking if some condition has already been concatenated you will have a criteria that starts with <nothing> AND <something>. This will give you an error of course.
Can´t set the parameter on the IF that build the query becausethe object "QUERY" doesn´t exists yet
The if is intended to be used after you create the query object obviously.
This solution violates DRY -- how can we define the query and set params at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.