users@glassfish.java.net

Re: merging entities with lazy loaded relationship

From: <glassfish_at_javadesktop.org>
Date: Mon, 28 Apr 2008 15:00:33 PDT

Hi Rebin,

I got the same problem and the only thing that you have to do is decorate the field with FetchType.EAGER. The explanation below:

[b]Lazy vs. Eager Field Bindings[/b]

By default, and for performance reasons, all field values are designated to be fetched
lazily, due to the fact that the implied fetch attribute found on each of the field mappings
(@Basic, @OneToMany, etc.) holds a default value of FetchType.LAZY. This default FetchType.
LAZY value is in fact only a hint, and the persistence manager is not bound to honor the
request. For many fields, including nearly all simple values, it would be a significant burden
to lazily fault in the fields of an entity as they are actually required, so the persistence
manager generally ignores the FetchType.LAZY directive and loads them eagerly anyhow.
When the non-default value FetchType.EAGER is specified on a field mapping, however,
this is not an optional request. When a field is so decorated, the persistence manager is
obliged to eagerly bind its value when the entity is instantiated. This is particularly relevant
when dealing with relationship fields. A relationship field may be annotated with the
fetch=FetchType.EAGER attribute to ensure that should the entity become detached, it will
still be possible for clients to traverse that relationship field to access the related entity
instances.
When an entity is managed, relationship values will be bound at the time they are
first requested. However, when an entity is instantiated and then detached, as when it
is serialized and passed to a remote client, it may be desirable to prebind all of its relationship
fields eagerly. In this case, you can override the default fetch values and set
(fetch=FetchType.EAGER) on the relationship fields. Be aware of the consequences of this
action, however, since this may cause a storm of cascaded loading if the eagerly loaded
collections in turn eagerly load their referenced objects, and so on.
[Message sent by forum member 'kreakone' (kreakone)]

http://forums.java.net/jive/thread.jspa?messageID=271898