Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java QueryDsl for "update myTable where myColumn in ('interesting', 'values')"?

Tags:

java

querydsl

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.

like image 644
SadBunny Avatar asked Oct 27 '25 16:10

SadBunny


1 Answers

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;

like image 178
Guillaume F. Avatar answered Oct 30 '25 07:10

Guillaume F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!