Let's say we have an entity
@Entity
public class Person {
@Id int id;
@Basic String name;
@Basic String remark;
}
Let's say "remark" field is filled with big texts, but rarely used. So it would be good if when you run jpql: SELECT p FROM Person p
, EclipseLink just executes sql select id, name from person
And than when you call person.getRemark()
, it will get fetched with select remark from person where id = ?
.
Is it possible with EclipseLink 2.1?
You can indeed define a fetch
attribute in a Basic
annotation and set it to LAZY
. But let me quote what the specification says about it:
11.1.6 Basic Annotation
(...)
The
EAGER
strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. TheLAZY
strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which theLAZY
strategy hint has been specified. In particular, lazy fetching might only be available forBasic
mappings for which property-based access is used.
In the particular case of EclipseLink, the behavior will depend on the context (Java EE vs Java SE) as explained in What You May Need to Know About EclipseLink JPA Lazy Loading.
In a Java EE environment (assuming the container implements the appropriate container contracts of the EJB 3.0 specification):
EclipseLink JPA performs lazy loading when the fetch attribute is set to javax.persistence.FetchType.LAZY.
In a Java SE environment:
By default, EclipseLink JPA ignores the fetch attribute and default javax.persistence.FetchType.EAGER applies.
To configure EclipseLink JPA to perform lazy loading when the fetch attribute set to FetchType.LAZY, consider one of the following:
- How to Configure Dynamic Weaving for JPA Entities Using the EclipseLink Agent
- How to Configure Static Weaving for JPA Entities
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With