Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EclipseLink: don't fetch some fields by default

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?

like image 750
Igor Mukhin Avatar asked Oct 14 '10 11:10

Igor Mukhin


1 Answers

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. The LAZY 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 the LAZY strategy hint has been specified. In particular, lazy fetching might only be available for Basic 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
like image 66
Pascal Thivent Avatar answered Oct 23 '22 11:10

Pascal Thivent