ebean icon indicating copy to clipboard operation
ebean copied to clipboard

Allow to configure fetch batch size when using FetchGroups

Open apflieger opened this issue 5 months ago • 1 comments

We use extensivly a pattern like this

DB.getDefault().createQuery(User.class).select(
        FetchGroup.of(User.class)
                .fetchQuery("tickets", FetchGroup.of(Ticket.class).build())
                .build()
);

But we can't configure this fetch on 'tickets' (like the fetch method allow on query with query.fetch(String, FetchConfig)). It might make sens to have:

DB.getDefault().createQuery(User.class).select(
        FetchGroup.of(User.class)
                .fetchQuery("tickets", FetchGroup.of(Ticket.class).build(), FetchConfig.ofQuery(1000))
                .build()
);

Or did I miss an alternative way to do this?

Btw thank you a lot for your work on this project that we use for years now 🙏

apflieger avatar Jan 25 '24 11:01 apflieger

We use extensively a pattern like this

FetchGroup can be static final so what I'd expect to see is instead:


// create the FetchGroup once
static final FetchGroup<User> MY_FETCH_GROUP = FetchGroup.of(User.class)
  . tickets.fetchQuery()
  .build()

Query<User> query = DB.getDefault().createQuery(User.class)
  .select(MY_FETCH_GROUP)
  .build()

But we can't configure this fetch ... [to use] FetchConfig.ofQuery(1000)

Yes, we are missing the ability to do this is a strong typed way like:

A) specify the FetchConfig

static final FetchGroup<User> MY_FETCH_GROUP = FetchGroup.of(User.class)
  .tickets.fetch(FetchConfig.ofQuery(1000)) // <!-- specify the FetchConfig here
  .build()

B) specify the FetchConfig with properties

static final QTicket TICKET = QTicket.alias();

static final FetchGroup<User> MY_FETCH_GROUP = FetchGroup.of(User.class)
  // specify the FetchConfig with properties
  .tickets.fetch(FetchConfig.ofQuery(1000), TICKET.price, TICKET.availableFrom) 
  .build()

alternative way to do this?

Yes, we can do this using the fetch() method on query beans that matches fetch(String,String,FetchConfig)


static final FetchGroup<User> MY_FETCH_GROUP = FetchGroup.of(User.class)
   // WORKAROUND (with specific properties)
  .fetch("tickets", "price, availableFrom", FetchConfig.ofQuery(1000))

   // or without properties
   // .fetch("tickets", FetchConfig.ofQuery(1000))
  .build()

rob-bygrave avatar Jan 25 '24 20:01 rob-bygrave

Supporting this via fetch(FetchConfig config, TQProperty<QB,?>... properties)

For example:

private static final FetchGroup<Customer> FETCHGROUP_CustomerWithContacts = 
      QCustomer.forFetchGroup()
        .select(cu.name, cu.phoneNumber)
        .contacts.fetch(FetchConfig.ofQuery(1000), co.firstName, co.lastName, co.email)
        .buildFetchGroup();


new QCustomer()
    .select(FETCHGROUP_CustomerWithContacts)
    .findList();

rbygrave avatar Mar 03 '24 00:03 rbygrave