views:

58

answers:

3

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?

+1  A: 

Try add annotation @Basic(fetch = FetchType.LAZY)

@Entity
public class Person {
    @Id int id;
    @Basic String name;
    @Basic(fetch = FetchType.LAZY) String remark;
}
nanda
Won't that still fetch `remark` every time a Person is fetched?
Tony Ennis
I haven't tried it, but according to http://wiki.eclipse.org/Introduction_to_EclipseLink_JPA_(ELUG)#.40Lob , EclipseLink should support lazy lob
nanda
A: 

We solved this problem (when using ActiveRecord and Hibernate) by putting the large string (usually a CLOB or BLOB) into it's own table with a FK to the main table (Person in this case.) Then it works like you want.

Tony Ennis
+1  A: 

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:

Pascal Thivent