3.13.1.3.1. Detached State
The openjpa.DetachState configuration property determines which fields and relations are detached by default.
All settings are recursive.
They are:
-
loaded: Detach all fields and relations that are already loaded, but don’t include unloaded fields in the detached graph. This is the default. -
fetch-groups: Detach all fields and relations in the current fetch configuration. For more information on custom fetch groups, see Fetch Groups. -
all: Detach all fields and relations. Be very careful when using this mode; if you have a highly-connected domain model, you could end up bringing every object in the database into memory!
Any field that is not included in the set determined by the detach mode is set to its Java default value in the detached instance.
The openjpa.DetachState option is actually a plugin string (see Plugin Configuration) that allows you to also configure the following options related to detached state:
-
DetachedStateField: As described in Attach Behavior above, OpenJPA can take advantage of a detached state field to make the attach process more efficient. This field is added by the enhancer and is not visible to your application. Set this property to one of the following values:-
transient: Use a transient detached state field. This gives the benefits of a detached state field to local objects that are never serialized, but retains serialization compatibility for client tiers without access to the enhanced versions of your classes or the OpenJPA runtime. This is the default. -
true: Use a non-transient detached state field so that objects crossing serialization barriers can still be attached efficiently. This requires, however, that your client tier have the enhanced versions of your classes and the OpenJPA runtime. -
false: Do not use a detached state field. No OpenJPA runtime will be required for client tiers.
-
The detached state field is also used to determine when proxies should be removed from entities during serialization. See the Serialization section for more details.
You can override the setting of this property or declare your own detached state field on individual classes using OpenJPA’s metadata extensions.
See Detached State Field below.
* DetachedStateManager: Whether to use a detached state manager. A detached state manager makes attachment much more efficient. Like a detached state field, however, it breaks serialization compatibility with the unenhanced class if it isn’t transient.
This setting piggybacks on the DetachedStateField setting above.
If your detached state field is transient, the detached state manager will also be transient.
If the detached state field is disabled, the detached state manager will also be disabled.
This is typically what you’ll want.
By setting DetachedStateField to true (or transient) and setting this property to false, however, you can use a detached state field without using a detached state manager.
This may be useful for debugging or for legacy OpenJPA users who find differences between OpenJPA’s behavior with a detached state manager and OpenJPA’s older behavior without one.
* AccessUnloaded: Whether to allow access to unloaded fields of detached objects. Defaults to true. Set to false to throw an exception whenever an unloaded field is accessed. This option is only available when you use detached state managers, as determined by the settings above.
* LiteAutoDetach: This option is ONLY valid for the loaded DetachState setting. Detach all fields and relations as described by the loaded property when an explicit detach is requested or when a single Entity is being detached as part of serialization. When the entire persistence context is being auto-detached ( openjpa.AutoDetach ), the minimal amount of work will be completed to disassociate all Entities from the persistence context. It is highly recommended that all Entities have a @Version field when using this property. In addition, care needs to be taken when this value is set to true as the following caveats apply:
-
A relationship from a managed Entity to an unmanaged Entity which was detached by the lite detach setting will not be persisted.
-
When merging a detached Entity back into the persistence context any lazily loaded fields that were marked to null when detached will not be persisted.
-
DetachProxyFields: This option is ONLY valid when used in conjunction with theLiteAutoDetachDetachState setting. When detaching the persistence context, all proxies will be left in place. Note, that allLarge Result Setswill be removed.
-
-
true(default): All proxies will be removed and LRS fields will be removed. -
false: All proxies will be left in place and LRS fields will be removed.
<property name="openjpa.DetachState" value="fetch-groups(DetachedStateField=true)"/>
You can also alter the set of fields that will be included in the detached graph at runtime. OpenJPAEntityManagers expose the following APIs for controlling detached state:
public DetachStateType getDetachState();
public void setDetachState(DetachStateType type);
The DetachStateType enum contains the following values:
enum DetachStateType {
FETCH_GROUPS,
LOADED,
ALL
}