Class ImportContext
PermazenTransaction
.
Plain objects (POJO's) can be imported into a PermazenTransaction
to the extent that the POJO class and
the corresponding Permazen model class share the same properties. The simplest example of this is when
the POJO class is also the Permazen model class (implying a non-abstract class; see also
@PermazenType.autogenNonAbstract()
). Also possible are POJO
classes and model classes that implement common Java interfaces.
The ObjId
for the corresponding imported Permazen object is determined by the configured objectIdMapper
.
If objectIdMapper
returns null, the POJO is not imported, and nulls replace any copied references to it; otherwise,
the returned object must exist in the transaction. If objectIdMapper
is itself null, the default behavior is
to create a new Permazen object using PermazenTransaction.create(Class)
, providing the POJO's class as the model class.
Instances ensure that an already-imported POJO will be recognized and not imported twice.
Note this determination is based on object identity, not Object.equals()
.
The objectIdMapper
is invoked at most once for any POJO.
Once the target object for a POJO has been identified, common properties are copied, overwriting any previous values. POJO properties that are not Permzen properties are ignored; conversely, properties that exist in the Permazen model object type but are missing in the POJO are left unmodified.
Permazen reference fields are automatically imported as POJO's, recursively. In other words, the entire transitive closure of POJO's reachable from an imported object is imported. Cycles in the graph of references are handled properly.
Conversion Details
Counter
fields import from any Number
property.- See Also:
-
Constructor Summary
ConstructorDescriptionConstructor.ImportContext
(PermazenTransaction ptx, Function<Object, ObjId> objectIdMapper) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionGet the mapping from already imported POJO's to their corresponding database objects.Get the destination transaction for imported objects.importPlain
(Object obj) Import a plain Java object (POJO), along with all other objects reachable from it via copied reference fields.
-
Constructor Details
-
ImportContext
Constructor.Uses a default
objectIdMapper
that creates new instances for imported objects viaPermazenTransaction.create(Class)
, using the Permazen model type found byPermazen.findPermazenClass(Class)
when given the POJO's class.- Parameters:
ptx
- the transaction in which to import objects- Throws:
IllegalArgumentException
- ifptx
is null
-
ImportContext
Constructor.- Parameters:
ptx
- the transaction in which to import objectsobjectIdMapper
- function assigningObjId
's to imported objects (or null to skip the corresponding object)- Throws:
IllegalArgumentException
- if either parameter is null
-
-
Method Details
-
getPermazenTransaction
Get the destination transaction for imported objects.- Returns:
- associated transaction
-
getObjectMap
Get the mapping from already imported POJO's to their corresponding database objects.- Returns:
- mapping from imported POJO to corresponding database object ID
-
importPlain
Import a plain Java object (POJO), along with all other objects reachable from it via copied reference fields.If
obj
has already been imported, the previously assignedPermazenObject
is returned.- Parameters:
obj
- object to import; must not be null- Returns:
- imported object, or null if the
objectIdMapper
returned null forobj
- Throws:
DeletedObjectException
- ifobjectIdMapper
returns the object ID of a non-existent objectTypeNotInSchemaException
- ifobjectIdMapper
returns an object ID that does not corresponding to any Permazen model classIllegalArgumentException
- ifobj
is null
-