FetchGroup

As an alternative to select and fetch we can use FetchGroup to specify what part of the object graph to fetch.

We can use FetchGroup with both query beans and standard queries.

FetchGroups provide a clean way to separate the definition of "what part of the object graph to load" (query tuning) from the definition of the query predicates (query business logic).

A FetchGroup is immutable, most often declared as a static final and can be combined to make other FetchGroups.

e.g. Just top level select properties

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

...

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

 

e.g. select and fetch properties

// 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();

...

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

Edit Page