Cloning
If you make a clone of a persistent instance, the clone is a separate
transient instance. The clone does not have a JDO identity and it is not
associated with the PersistenceManager
of the instance that was
cloned. The clone is a shallow copy of the original instance, without
regard for the persistent fields. Therefore, the fields might not have
been fetched from the datastore yet, causing you to get a null
for fields that are references, including
types like Integer
and references to
other persistent instances. Normally, the fields in the default fetch
group have been fetched from the datastore, but not always. You should
therefore call retrieve( )
to make sure the field values have been fetched from the
datastore.
Another issue to consider is that the persistent instance may have
references to other persistent instances. For example, a RentalItem
has a reference to a MediaItem
. If we retrieve all the fields of a
RentalItem
instance and then create a
clone of it, the clone will have a reference to the MediaItem
, but this clone is transient and
does not really have a relationship with the MediaItem
instance. JDO has a well-defined
behavior that allows implementations to create a clone of a persistent
instance properly, but we recommend that you do not clone persistent
instances.
Get Java Data Objects now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.