3.6.6.4.3. Custom Proxies
OpenJPA manages proxies through the
org.apache.openjpa.util.ProxyManager interface.
OpenJPA includes a default proxy manager, the org.apache.openjpa.util.ProxyManagerImpl (with a plugin alias name of default), that will meet the needs of most users.
The default proxy manager understands the following configuration properties:
-
TrackChanges: Whether to use smart proxies. Defaults totrue. -
AssertAllowedType: Whether to immediately throw an exception if you attempt to add an element to a collection or map that is not assignable to the element type declared in metadata. Defaults tofalse. -
DelayCollectionLoading: Whether to delay loading elements of a lazily loaded collection. Delay loading allows non-indexed add and remove operations to occur without prior loading of the collection from the data store. This can improve performance of some applications by allowing them to perform simple add or remove operations on collections without requiring them to be loaded. Delayed proxies are loaded when an operation is performed that requires loading, such as iteration, size, serialization, and indexOf. They can also be loaded by casting the proxy to a org.apache.openjpa.util.proxy.DelayedProxy and invoking theloadmethod. If a broker factory is available after detaching the owning entity, a collection may be available for delayed loading after the persistence context has been cleared. In post-detachment, entities that are loaded are not associated with a persistence context. Defaults tofalse.
The default proxy manager can proxy the standard methods of any Collection, List, Map, Queue, Date, or Calendar class, including custom implementations.
It can also proxy custom classes whose accessor and mutator methods follow JavaBean naming conventions.
Your custom types must, however, meet the following criteria:
-
Custom container types must have a public no-arg constructor or a public constructor that takes a single
Comparatorparameter. -
Custom date types must have a public no-arg constructor or a public constructor that takes a single
longparameter representing the current time. -
Other custom types must have a public no-arg constructor or a public copy constructor. If a custom types does not have a copy constructor, it must be possible to fully copy an instance A by creating a new instance B and calling each of B’s setters with the value from the corresponding getter on A.
If you have custom classes that must be proxied and do not meet these requirements, OpenJPA allows you to define your own proxy classes and your own proxy manager.
See the openjpa.util package Javadoc for details on the interfaces involved, and the utility classes OpenJPA provides to assist you.
You can plug your custom proxy manager into the OpenJPA runtime through the openjpa.ProxyManager configuration property.
<property name="openjpa.ProxyManager" value="TrackChanges=false"/>