I have the urge to migrate a bunch of JPA data from Derby to MySQL. After fighting typos for an hour I eventually started hitting actual roadblocks.
Simply fetching all the objects from the source database and using persist()
on the new database fails because the object belongs to a different context.
Exception in thread "main" <openjpa-2.4.0-r422266:1674604 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: The given instance "com.purplefrog.dvdlibrarian.LabelledTrack-101" is not managed by this context.
FailedObject: com.purplefrog.dvdlibrarian.LabelledTrack-101
If you change the object to have a clone()
method you get
Exception in thread "main" <openjpa-2.4.0-r422266:1674604 nonfatal store error> org.apache.openjpa.persistence.EntityExistsException: Attempt to persist detached object "com.purplefrog.dvdlibrarian.LabelledTrack@164a62bf". If this is a new instance, make sure any version and/or auto-generated primary key fields are null/default when persisting.
FailedObject: com.purplefrog.dvdlibrarian.LabelledTrack@164a62bf
So modify the clone()
method to zero out the id field and then it works.
My gripe is that this requires modifying every object to have a clone()
method.
My gut feeling is that using reflection to identify all of the @Column
s and use the getters and setters is a cleaner solution. Is there already a utility function capable of cloning an arbitrary JPA object using reflection, or do I have to write my own?
Aucun commentaire:
Enregistrer un commentaire