Enum Class FetchMethod

java.lang.Object
java.lang.Enum<FetchMethod>
org.hibernate.FetchMethod
All Implemented Interfaces:
FetchOption, Serializable, Comparable<FetchMethod>, Constable

@Incubating public enum FetchMethod extends Enum<FetchMethod> implements FetchOption
Enumerates methods for fetching an association from the database, allowing a fetching method to be specified as a FetchOption of an attribute node of an entity graph.
var bookWithAuthors = session.createEntityGraph(Book.class);
bookWithAuthors.addAttributeNode(Book_.authors) // fetch authors
               .addOption(FetchMethod.SELECT);  // using separate SQL select
Book book = session.get(bookWithAuthors, isbn);

The JPA-defined FetchType enumerates the possibilities for when an association might be fetched. This enumeration defines how fetching is realized in terms of the actual SQL executed by the database.

Since:
8.0
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class Enum

    Enum.EnumDesc<E>
  • Enum Constant Summary

    Enum Constants
    Enum Constant
    Description
    Use a secondary select to load a single associated entity or collection, at some point after an initial query is executed.
    Use a secondary select with a subselect that re-executes an initial query to load all instances of the related entity or collection at once, at some point after the initial query is executed.
    Use an outer join to load all instances of the related entity or collection at once, as part of the execution of a query.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the enum constant of this class with the specified name.
    static FetchMethod[]
    Returns an array containing the constants of this enum class, in the order they are declared.

    Methods inherited from class Object

    getClass, notify, notifyAll, wait, wait, wait
  • Enum Constant Details

    • BY_ID

      public static final FetchMethod BY_ID
      Use a secondary select to load a single associated entity or collection, at some point after an initial query is executed.

      This is the default fetching strategy for any association or collection in Hibernate, unless the association or collection is explicitly marked for eager fetching.

      This fetching strategy is vulnerable to the "N+1 selects" bugbear, though the impact may be alleviated somewhat via:

      • enabling batch fetching using BatchSize, or
      • ensuring that the associated entity or collection may be retrieved from the second-level cache.

      This fetching strategy is, in principle, compatible with both eager and lazy fetching. On the other hand, performance considerations dictate that it should only be used in combination with EAGER fetching when it is almost certain that the associated data will be available in the second-level cache.

    • JOIN

      public static final FetchMethod JOIN
      Use an outer join to load all instances of the related entity or collection at once, as part of the execution of a query. No subsequent queries are executed.

      This is the default fetching strategy for an association or collection that is explicitly marked for eager fetching.

      This fetching strategy is incompatible with lazy fetching since the associated data is retrieved as part of the initial query.

    • BY_SUBQUERY

      public static final FetchMethod BY_SUBQUERY
      Use a secondary select with a subselect that re-executes an initial query to load all instances of the related entity or collection at once, at some point after the initial query is executed.

      This advanced fetching strategy is compatible with both eager and lazy fetching.

      Subselect fetching may be contrasted with batch fetching:

      • In batch fetching, a list of primary key values is sent to the database, bound within a SQL in condition.
      • In subselect fetching, the primary keys are determined by re-execution of the initial query within a SQL subselect.
  • Method Details

    • values

      public static FetchMethod[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static FetchMethod valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null