Update query

UpdateQuery allows us to perform a bulk sql update by creating a query via query beans or just as a standard ORM query.

asUpdate

Use asUpdate() to convert a query into an UpdateQuery after which point we can set update parameters via set() and setRaw().

Example 1
int rows = new QCustomer()
  .name.startsWith("Rob")
  .asUpdate()                      // convert to UpdateQuery
    .set("registered", now)        // update set ...
    .update();
update customer set registered=? where name like ? escape'|'

 

Example 2
int rows = new QCustomer()
  .billingAddress.country.equalTo(nz)
  .billingAddress.city.equalTo("Auckland")
  .name.startsWith("Rob")
  .asUpdate()
    .set("registered", now)
    .setRaw("name = concat(?, name, '+', ?)", "before", "after")
    .update();
update customer set registered=?, name = concat(?, name, '+', ?)
where id in (
  select t0.id from customer t0
  left join address t1 on t1.id = t0.billing_address_id
  where t1.country_code = ?  and t1.city = ?  and t0.name like ? escape'|'
);
--bind(2019-01-18 17:12:01.348,before,after,NZ,Auckland,Rob%) rows:0

Compare SqlUpdate

If we want to use SQL directly we can instead use SqlUpdate.

Related: Delete query SqlUpdate