public class ExportContext extends Object
JTransaction
.
Plain objects (POJO's) can be exported from a JTransaction
to the extent that the Permazen model class and
the corresponding target POJO class share the same properties. The simplest example of this is when the Permazen model class
is also the POJO class (implying a non-abstract class; see also
@PermazenType.autogenNonAbstract()
). Also possible are POJO
classes and model classes that implement common Java interfaces.
The POJO corresponding to an exported database object is supplied by the configured objectMapper
.
If objectMapper
returns null, the database object is not exported, and nulls replace any copied references to it.
If objectMapper
is null, the default behavior is to create a new POJO using the model class' default constructor,
which of course implies the model class cannot be abstract.
Instances ensure that an already-exported database object will be recognized and not exported twice.
The objectMapper
is invoked at most once for any object ID.
When a database objext is exported, its fields are copied to the POJO. Fields for which no corresponding POJO property exists are omitted.
Reference fields are traversed and the referenced objects are automatically exported as POJO's, recursively. In other words, the entire transitive closure of objects reachable from an exported object is exported. Cycles in the graph of references are handled properly.
Conversion Details
Counter
fields export to any Number
property. Collection fields export to an existing collection,
so that a setter method is not required; however, if the getter returns null, a setter is required and the export
will attempt to use an appropriate collection class (HashSet
for property of type Set
,
TreeSet
for a property of type SortedSet
, etc). To avoid potential mismatch with
collection types, initialize collection properties.ImportContext
Constructor and Description |
---|
ExportContext(JTransaction jtx)
Constructor.
|
ExportContext(JTransaction jtx,
Function<ObjId,Object> objectMapper)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
Object |
exportPlain(JObject jobj)
Export a
JObject as a plain Java object, along with all other objects reachable from it via
copied reference fields. |
Object |
exportPlain(ObjId id)
|
Map<ObjId,Object> |
getJObjectMap()
Get the mapping from already exported database object to the corresponding POJO.
|
JTransaction |
getTransaction()
Get the transaction from which objects are exported.
|
public ExportContext(JTransaction jtx)
Uses a default objectMapper
that creates new exported objects using the default constructor of the model class.
jtx
- the transaction from which to export objectsIllegalArgumentException
- if jtx
is nullpublic ExportContext(JTransaction jtx, Function<ObjId,Object> objectMapper)
jtx
- the transaction from which to export objectsobjectMapper
- function returning the POJO used to export a database object (or null to skip the corresponding object)IllegalArgumentException
- if either parameter is nullpublic JTransaction getTransaction()
public Map<ObjId,Object> getJObjectMap()
public Object exportPlain(JObject jobj)
JObject
as a plain Java object, along with all other objects reachable from it via
copied reference fields.
Equivalent to exportPlain
(jobj.getObjId())
.
jobj
- object to export; must not be nullobjectMapper
returned null for jobj.getObjId()
DeletedObjectException
- if id
refers to an object that does not exist
in the transaction associated with this instanceTypeNotInSchemaVersionException
- if jobj
is an UntypedJObject
IllegalArgumentException
- if jobj
is nullpublic Object exportPlain(ObjId id)
JObject
with the given ObjId
as a plain Java object, along with all other objects
reachable from it via copied reference fields.
If the JObject
has already been exported, the previously returned Object
is returned.
id
- object ID of the object to export; must not be nullobjectMapper
returned null for id
DeletedObjectException
- if id
refers to an object that does not exist
in the transaction associated with this instanceTypeNotInSchemaVersionException
- if id
refers to a type that does not exist
in this instance's transaction's schema versionIllegalArgumentException
- if id
is nullCopyright © 2022. All rights reserved.