エイリアス
基本テーブルで使用するテーブルエイリアスを設定します。
List<Customer> customers =
new QCustomer()
.alias("cust")
.name.istartsWith("Rob")
.findList();
select cust.id, cust.inactive, cust.name, cust.registered, ...
from customer cust
where lower(cust.name) like ? escape'|' ; --bind(rob%)
通常エイリアスを使用するのは、サブクエリとして使用する場合です。
Query<Order> subQuery = new QOrder()
.alias("sub")
.select("id").where().raw("sub.customer_id = main.id")
.query();
List<Customer> customers = new QCustomer()
.alias("main")
.query().where().exists(subQuery)
.findList();
select main.id, main.inactive, main.name, main.registered, ...
from customer main
where exists (select sub.id from orders sub where sub.customer_id = main.id)
ドラフトとして
@Draftable
エンティティを、公開テーブルではなくドラフトテーブルから取得するために使用します。
Document documentDraft =
new QDocument()
.asDraft()
.id.equalTo(42)
.findOne();
タイムスタンプ
@History
を備えたエンティティの場合、エンティティを指定されたタイムスタンプのAS OF
で取得します。
Timestamp timeInPast = ...;
List<Customer> customers
= new QCustomer()
.asOf(timeInPast)
.findList();
自動チューニング
自動クエリチューニングをオンまたはオフします。
List<|Customer> customers = new QCustomer() .setAutoTune(true) // クエリを自動的にチューニング .status.equalTo(Status.NEW) .findList();バッファフェッチサイズヒント
ステートメントのfetchSize()
に変換される、JDBC用のヒントです。
ResultSetにさらに多くの行が必要な場合に、データベースから取得する行の数を、JDBCドライバに示唆します。
遅延ロードの無効化
遅延ロードが発生する可能性があるものを無効にします。代わりに、アンロードされたプロパティはnullを返します。
読み取り監査の無効化
このクエリによる読み取り監査を無効にします。読み取り監査を参照してください。
クエリがユーザーによって開始されたものではなく、キャッシュやドキュメントストアなどをロードするためのアプリケーションの内部処理の一部である場合を対象としています。このような場合、クエリを更新監査の一部とすることは望ましくありません。
重複除去
クエリにsql distinct
を使用させます。
List<String> lastNames =
new QContact()
.setDistinct(true)
.select("lastName")
.email.isNull()
.findSingleAttributeList();
int count =
new QCustomer()
.setDistinct(true)
.select("anniversary")
.status.equalTo(Customer.Status.NEW)
.findCount();
select count(distinct t0.anniversary) from customer t0 where t0.status = ?
// partially loaded beans without Id properties
// ... not persistable, no lazy loading etc
List<Customer> beans =
new QContact()
.setDistinct(true)
.select("lastName, dateOfBirth")
.findList();
更新用
取得した行をロックするFOR UPDATE
でクエリを実行します。
try (Transaction txn = database.beginTransaction()) {
Customer customer =
new QContact()
.forUpdate()
.email.equalTo("rob@foo.com")
.findOne();
customer.setName("a better name");
customer.save();
txn.commit();
}
ソフト削除のインクルード
ソフト削除された行をインクルードするように設定します。ソフト削除を参照してください。
遅延ロードのバッチサイズ
デフォルトで10
に設定されている、遅延ロードのバッチサイズを調整するために設定します。
パーシスタンスコンテキストスコープ
通常、クエリはトランザクションスコープされたパーシスタンスコンテキスト
で実行されます。これをQUERY
に設定すると、構築されたオブジェクトグラフはデータベースから取得した最新データになり、[トランザクション]パーシスタンスコンテキストにある既存のBeanは無視されます。
try (Transaction txn = database.beginTransaction()) {
// when customer is loaded it then is referenced in the
// transaction scoped persistence context (aka L1 cache)
Customer loaded = Customer.find.byId(42)
// fetch this same customer but we want to ignore the L1 cache
// such that we get a "fresh" version of its data
Customer customer =
new QCustomer()
.id.equalTo(42)
.setUseCache(false) // ignore L2 cache
.setPersistenceContextScope(QUERY) // ignore L1 cache
.findOne();
...
txn.commit();
}
読み取り専用
クエリは読み取り専用
とみなされるBeanを返し、セッターメソッドを呼び出すと例外が発生します。