フェッチグループ

selectfetchの代わりに、部分のオブジェクトグラフをフェッチするFetchGroupを使用できます。

FetchGroupはクエリBeanと標準クエリの両方で使用できます。

フェッチグループは、「ロードするオブジェクトグラフの部分」(クエリのチューニング)の定義を、クエリ述語の定義(クエリのビジネスロジック)から分離する明確な方法を提供します。

フェッチグループは不変で、ほとんどの場合static finalとして宣言され、他のフェッチグループを組み合わせて作成できます。

例:トップレベルの選択プロパティのみ

// immutable and threadsafe

static final QCustomer CUST = QCustomer.alias();

static final FetchGroup<Customer> fetch = QCustomer.forFetchGroup()
    .select(CUST.name, CUST.version, CUST.whenCreated)
    .buildFetchGroup();

...

List<Customer> customers =
  new QCustomer()
    .select(fetch)
    .name.istartsWith("Rob")
    .findList();

上記のFetchGroupは、次のクエリBeanを使用せずに作成できます

static final FetchGroup<Customer> fetch =
    FetchGroup.of(Customer.class, "name, version, whenCreated"); // root level properties

 

例:selectとfetchのプロパティ

// immutable and threadsafe

static final QCustomer CUST = QCustomer.alias();
static final QContact  CONT = QContact.alias();

static final FetchGroup<Customer> fetch = QCustomer.forFetchGroup()
    .select(CUST.name, CUST.version, CUST,whenCreated)
    .contacts.fetch(CONT.email)          // fetch the contacts with just their email
    .billingAddress.fetch()              // fetch all properties of billingAddress
    .buildFetchGroup();

...

List<Customer> customers =
  new QCustomer()
    .select(fetch)
    .name.istartsWith("Rob")
    .findList();

上記のFetchGroupは、クエリBeanを使用せずに作成できます

// immutable and threadsafe
static final FetchGroup<Customer> fetch =
   FetchGroup.of(Customer.class)
    .select("name, status")       // root level properties
    .fetch("contacts", "email")   // associated bean properties
    .build();

これは、関連する行、出荷、顧客、顧客.請求先住所、および顧客.連絡先を持つ注文を取得する大きな例です。

各パスでどのプロパティ(またはすべて)をフェッチするかを制御することに注意してください。

この例では、複数のToManyパスがあり、fetchQueryを使用してグラフを作成するためにORMクエリを複数のSQLクエリに分割する方法を明示的に制御しています。

// immutable and threadsafe

static final QOrder ORD = QOrder.alias();
static final QCustomer CUST = QCustomer.alias();
static final QContact CONT = QContact.alias();

static final FetchGroup<Order> fetch = QOrder.forFetchGroup()
    .customer.fetch(CUST.name, CUST.version, CUST,whenCreated)
    .customer.shippingAddress.fetch()
    .customer.contacts.fetch(CONT.email)                        // a ToMany path
    .lines.fetchQuery()                                         // a ToMany path
    .shipments.fetchQuery()                                     // a ToMany path
    .buildFetchGroup();

...

List<Order> orders =
  new QOrder()
    .select(fetch)
    .status.eq(Order.Status.NEW)
    .findList();