Update query

UpdateQueryを使用すると、クエリBeanまたは標準のORMクエリを介して一括SQL更新を実行できます。

asUpdate

asUpdate()を使用して、クエリをUpdateQueryに変換します。その後、set()setRaw()を使用して更新パラメーターを設定できます。

例 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'|'

 

例 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

SqlUpdateとの比較

SQLを直接使用したい場合は、SqlUpdateを使用できます。

関連:Delete query SqlUpdate