jakarta-eesessionfactorycreatequery

SetParametres in Query


I'm trying to set a query with lot of parameters.

for a query with 1 param its working fine:

//**
    List<?> list = null;
    String query = String.format("from %s where %s = :%s ", a, uniqueField[0], uniqueField[0]);
    list = s.createQuery(query).setParameter(uniqueField[0], arg0.getSsn()).list();

how can i do the same with this query :

List<?> list = null;
String query = String.format("from %s where %s = :%s and %s = :%s ", a, uniqueField[0], uniqueField[0], uniqueField[1], uniqueField[1]);
// list = s.createQuery(query)... ?

Thanks for any advices..


Solution

  • When using a query, you can either use named parameters or positional parameters. Named parameters are pretty easy, I'm not sure what the performance implications are:

    So, just name the parameters then: ":param1", ":param2". Then q.setParameter(":param1", value1).setParam(":param2", value2).

    Reference: In JPA which type of parameter is better to use “positional/named”?