Java Querydsl for "Update myTable Where myColumn in ('Interesting', 'Values')"?

Java Querydsl for "Update myTable Where myColumn in ('Interesting', 'Values')"?

I'm trying to translate this query in QueryDsl:

update myThings set firstColumn = 'newValue' where secondColumn in ('interesting', 'stuff')

I spent hours looking for documentation but the java fu is just not strong enough in this one... :( I can find all kinds of QueryDsl example, but I cant find any for this. I will probably need SimpleExpression.eqAny(CollectionExpression), but I can't figure out how to build such a CollectionExpression around my simple list of strings.

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

All I can find is API definitions, but then I get lost in generics any other "new" java concepts which I still have trouble understanding. An example would be very much appreciated.

1 Answer

You have to use new JPAUpdateClause(session, myThings):

JPAUpdateClause<myThings> update = new JPAUpdateClause(session, myThings);
update.set(myThings.firstColumn, "newValue")
      .where(myThings.secondColumn.in(interestingValues))
      .execute();

If you are using hibernate, use HibernateUpdateClause() instead;

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.