简体   繁体   中英

QueryDsl queries with parameters?

With jpa we have the NamedQuery witch alow us to pass the parameters later this way:

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, is there a similar way to pass parameters later with QueryDsl?

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"));

That should be trivial to adapt the code to static generated QueryDSL beans.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM