13.2 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.



Java Data Objects
ADO.NET Programming with CDR (Wordware programming library)
ISBN: 596002769
EAN: 2147483647
Year: 2005
Pages: 159

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net