我正在尝试在 QueryDsl 中翻译这个查询:
update myThings set firstColumn = 'newValue' where secondColumn in ('interesting', 'stuff')
我花了几个小时寻找文档,但 java fu 在这方面还不够强大... :( 我可以找到各种 QueryDsl 示例,但我找不到任何一个。我可能需要 SimpleExpression.eqAny (CollectionExpression),但我不知道如何围绕我的简单字符串列表构建这样一个 CollectionExpression。
List<String> interestingValues = Arrays.asList("interesting", "stuff");
queryFactory.update(myThings)
.set(myThings.firstColumn, "newValue")
// .where(myThings.secondColumn.in(interestingValues)
// 'in' will probably try to look in table "interestingValues"?
// .where(myThings.secondColumn.eqAny(interestingValues)
// 'eqAny' seems interesting, but doesn't accept a list
.execute();
我所能找到的只是 API 定义,但后来我迷失在泛型中,其他任何我仍然难以理解的"new"Java 概念。非常感谢一个例子。
最佳答案
你必须使用new JPAUpdateClause(session, myThings)
:
JPAUpdateClause<myThings> update = new JPAUpdateClause(session, myThings);
update.set(myThings.firstColumn, "newValue")
.where(myThings.secondColumn.in(interestingValues))
.execute();
如果您正在使用 hibernate ,请改用HibernateUpdateClause()
;
关于 "update myTable where myColumn in (' 有趣的 Java QueryDsl', 'values' )”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34239863/