Introduction To JPAFetch Types
Introduction To JPAFetch Types
What is a FetchType
The FetchType defines when Hibernate gets the related entities from
the database, and it is one of the crucial elements for a fast
persistence tier.
In general, you want to fetch the entities you use in your business
tier as efficiently as possible. But that’s not that easy. You either get
all relationships with one query or you fetch only the root entity and
initialize the relationships as soon as you need them.
Default FetchTypes
The default depends on the cardinality of the relationship. All to-one
relationships use FetchType.EAGER and all to-many relationships
FetchType.LAZY.
...
www.thoughts-on-java.org
JPA FetchTypes
FetchType.EAGER – Fetch it so you’ll have it when you need it
The FetchType.EAGER tells Hibernate to get all elements of a
relationship when selecting the root entity.
www.thoughts-on-java.org
JPA FetchTypes
FetchType.LAZY – Fetch it when you need it
The FetchType.LAZY tells Hibernate to only fetch the related entities
from the database when you use the relationship. This is a good idea
in general because there’s no reason to select entities you don’t need
for your uses case.
The used FetchType has no influence on the business code. You can call
the getter method just as any other getter method.
www.thoughts-on-java.org