QueryDsl queries with parameters?

With jpa, we have NamedQuery so that we can pass parameters later as follows:

public <T2> T2 getSingleResult(String namedQuery, Map<String, String> parameters, Class<T2> clazz) {

    TypedQuery<T2> typedQuery = entityManager.createNamedQuery(namedQuery, clazz);
    for (Entry<String, String> parameter : parameters.entrySet()) {
        typedQuery.setParameter(parameter.getKey(), parameter.getValue());
    }
    return typedQuery.getSingleResult();
}

So, I want to know if there is a similar way to pass parameters later with QueryDsl?

+4
source share
1 answer

I use the following approach with PathBuilder:

PathBuilder pathBuilder = new PathBuilder(Object.class, "my_table");
SQLQuery query = new SQLQuery(connection, OracleTemplates.DEFAULT);
query.from(pathBuilder.getRoot())
    .where(pathBuilder.get("my_column").eq(new Param(String.class, "param1")))
    .set(new Param(String.class, "param1"), "67")
    .list(pathBuilder.get("my_column"));

It must be trivial to adapt the code to the static QueryDSL beans generator.

0
source

Source: https://habr.com/ru/post/1614917/


All Articles