Annotation Type FetchPreference


@Retention(RUNTIME) @Target(FIELD) public @interface FetchPreference
Used to control which ToMany relationships are preferred as 'joins' rather than as 'secondary joins' (query fetches).

Ebean automatically limits the number of SQL joins to ToMany relationships to avoid SQL cartesian product and to honor SQL first row / max rows limits. It does this by allowing the first ToMany relationship/path that has been defined on a query to be included in the main query as a join and then converting any remaining ToMany relationship/path to "query joins".

Without explicit use of @FetchPreference the first path that contains a ToMany is included in the main query as a join.

Indicate to Ebean the preferred relationships to join to.

Example

Prefer joins to 'participants' over 'messages'



     @FetchPreference(1)
     @OneToMany(mappedBy = "conversation")
     List<Participation> participants;

     @FetchPreference(2)
     @OneToMany(mappedBy = "conversation")
     List<Message> messages;

 

Example query that is impacted



  // Without @FetchPreference the first ToMany which
  // is "messages" is joined and "participants" is
  // query joined (executed as a secondary query)

  Ebean.find(Conversation.class)
      .fetch("messages")         // <- first ToMany path Ebean sees
      .fetch("participants")     // <- second ToMany path Ebean sees
      .findList();

 
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    int
    The fetch preference used - low value means higher preference.
  • Element Details

    • value

      int value
      The fetch preference used - low value means higher preference.