Enum Class FetchType

java.lang.Object
java.lang.Enum<FetchType>
jakarta.persistence.FetchType
All Implemented Interfaces:
FetchOption, Serializable, Comparable<FetchType>, Constable

public enum FetchType extends Enum<FetchType> implements FetchOption
Defines policies for fetching data from the database.
  • The EAGER policy is a requirement on the persistence provider runtime that data must be eagerly fetched.
  • The LAZY policy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed, in the case of a managed entity associated with a persistence context, or on a call to EntityAgent.fetch(T), in the case of a detached entity. The implementation is permitted to eagerly fetch data for which the LAZY policy hint has been specified.
  • A DEFAULT value specifies that the fetching policy depends on the default fetch type of the persistence unit for one-to-one and many-to-one associations. This default fetch type is controlled via the default-to-one-fetch-type element in the persistence.xml file or by setting the defaultToOneFetchType of the PersistenceConfiguration, and defaults to EAGER for backward compatibility.

This example specifies that a ManyToOne association should, by default, be fetched lazily on first access:

@ManyToOne(fetch = LAZY)
private Publisher publisher;

This example specifies that an ElementCollection is fetched eagerly:

@ElementCollection(fetch = EAGER)
Set<String> topics;

The use of fetch = EAGER often leads to fetching of unwanted data and is therefore discouraged. Eager fetching may be explicitly requested precisely where needed using an EntityGraph or a query with left join fetch.

Since:
1.0
See Also:
API note:
It is very strongly recommended that new applications set the default fetch type for one-to-one and many-to-one associations to LAZY to avoid accidental fetching of unnecessary data.
  • Enum Constant Details

    • LAZY

      public static final FetchType LAZY
      Specifies that data can be lazily fetched.

      This fetching policy is a hint to the provider. A persistence provider is always permitted to eagerly fetch more data than what is explicitly requested by the application.

    • EAGER

      public static final FetchType EAGER
      Specifies that data must be eagerly fetched.

      This fetching policy is a requirement on the persistence provider.

    • DEFAULT

      public static final FetchType DEFAULT
      Specifies that the fetching policy depends on the default fetch type of the persistence unit for one-to-one and many-to-one associations.
      Since:
      4.0
      API note:
      It is very strongly recommended that new applications set the default fetch type to LAZY to avoid accidental fetching of unnecessary data.
  • Method Details

    • values

      public static FetchType[] 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 FetchType 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