public class ImportContext extends Object
JTransaction
.
Plain objects (POJO's) can be imported into a JTransaction
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 storageIdMapper
.
If storageIdMapper
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 storageIdMapper
is itself null, the default behavior is
to create a new Permazen object using JTransaction.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 storageIdMapper
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.ExportContext
Constructor and Description |
---|
ImportContext(JTransaction jtx)
Constructor.
|
ImportContext(JTransaction jtx,
Function<Object,ObjId> storageIdMapper)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
Map<Object,ObjId> |
getObjectMap()
Get the mapping from already imported POJO's to their corresponding database objects.
|
JTransaction |
getTransaction()
Get the destination transaction for imported objects.
|
JObject |
importPlain(Object obj)
Import a plain Java object (POJO), along with all other objects reachable from it via copied reference fields.
|
public ImportContext(JTransaction jtx)
Uses a default storageIdMapper
that creates new instances for imported objects via
JTransaction.create(Class)
, using the Permazen model type found by
Permazen.findJClass(Class)
when given the POJO's class.
jtx
- the transaction in which to import objectsIllegalArgumentException
- if jtx
is nullpublic ImportContext(JTransaction jtx, Function<Object,ObjId> storageIdMapper)
jtx
- the transaction in which to import objectsstorageIdMapper
- function assigning ObjId
's to imported objects (or null to skip the corresponding object)IllegalArgumentException
- if either parameter is nullpublic JTransaction getTransaction()
public Map<Object,ObjId> getObjectMap()
public JObject importPlain(Object obj)
If obj
has already been imported, the previously assigned JObject
is returned.
obj
- object to import; must not be nullstorageIdMapper
returned null for obj
DeletedObjectException
- if storageIdMapper
returns the object ID of a non-existent objectTypeNotInSchemaVersionException
- if storageIdMapper
returns an object ID that does not
corresponding to any Permazen model classIllegalArgumentException
- if obj
is nullCopyright © 2022. All rights reserved.