Class ExportContext
PermazenTransaction.
Plain objects (POJO's) can be exported from a PermazenTransaction 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.- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor.ExportContext(PermazenTransaction ptx, Function<ObjId, Object> objectMapper) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionexportPlain(ObjId id) Export thePermazenObjectwith the givenObjIdas a plain Java object, along with all other objects reachable from it via copied reference fields.exportPlain(PermazenObject pobj) Export aPermazenObjectas a plain Java object, along with all other objects reachable from it via copied reference fields.Get the mapping from already exported database object to the corresponding POJO.Get the transaction from which objects are exported.
-
Constructor Details
-
ExportContext
Constructor.Uses a default
objectMapperthat creates new exported objects using the default constructor of the model class.- Parameters:
ptx- the transaction from which to export objects- Throws:
IllegalArgumentException- ifptxis null
-
ExportContext
Constructor.- Parameters:
ptx- the transaction from which to export objectsobjectMapper- function returning the POJO used to export a database object (or null to skip the corresponding object)- Throws:
IllegalArgumentException- if either parameter is null
-
-
Method Details
-
getPermazenTransaction
Get the transaction from which objects are exported.- Returns:
- associated transaction
-
getPermazenObjectMap
Get the mapping from already exported database object to the corresponding POJO.- Returns:
- mapping from exported database object ID to corresponding POJO
-
exportPlain
Export aPermazenObjectas a plain Java object, along with all other objects reachable from it via copied reference fields.Equivalent to
exportPlain(pobj.getObjId()).- Parameters:
pobj- object to export; must not be null- Returns:
- exported object, or null if the
objectMapperreturned null forpobj.getObjId() - Throws:
DeletedObjectException- ifidrefers to an object that does not exist in the transaction associated with this instanceTypeNotInSchemaException- ifpobjis anUntypedPermazenObjectIllegalArgumentException- ifpobjis null
-
exportPlain
Export thePermazenObjectwith the givenObjIdas a plain Java object, along with all other objects reachable from it via copied reference fields.If the
PermazenObjecthas already been exported, the previously returnedObjectis returned.- Parameters:
id- object ID of the object to export; must not be null- Returns:
- exported object, or null if the
objectMapperreturned null forid - Throws:
DeletedObjectException- ifidrefers to an object that does not exist in the transaction associated with this instanceTypeNotInSchemaException- ifidrefers to a type that does not exist in this instance's transaction's schemaIllegalArgumentException- ifidis null
-