Is there any restriction when using fetch=FetchType.LAZY ?

#1

Hi, I came across a weird behavior when using fetch=FetchType.LAZY in a complex code. However I am  unable to reproduce the behavior in a simple code.

The behavior is like below:

1. When I am using in object declaration

@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private LinkedList<OdbObject> referencedObj;

My test suite is running well.

2. However when I change fetch to LAZY

@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private LinkedList<OdbObject> referencedObj;

It seems referencedObj is becoming null for a certain condition. (I checked by inserting code at @PreUpdate). 

The referencedObj is becoming null is after executing the following line:

em.detach(this);

 

Regards,

--

eko

 

 

#2

You can use FetchType.LAZY on any field (although it is only a hint in JPA and ObjectDB is allowed to ignore it and load data eagerly).

On detach some unloaded fields (inverse / mapped-by relationships) are nullified by ObjectDB to signal that they have not been loaded yet, as detached objects cannot handle automatic loading on access. See this thread and the objectdb.temp.no-detach mode.

ObjectDB Support

Reply