我有一个需要使用“动态”参数集的查询。但是无法意识到该怎么做,我试图不添加 where 字符串,但它给了我错误,因为参数已设置。
有小费吗?
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();