I'm trying to copy a graph of 34 different kinds of objects which form a variety of lattices, trees, and other relationships. The copy has to work on a single server as well as across different servers connected to other databases (with the same schema and Java classes with identical SerialVersionUids). My most promising method to date is to load the whole graph into memory (probably max 50MB), then serialize it. This part works, though I haven't initialized absolutely everything and there are a few PersistentSets and AbstractPersistantCollections and AbstractComponentTypes in my serialized file. Will those blow up if I send them to a different server? That's not the only problem...
On the other side, I read the file in and do a hiberSession.save(parent) with all my hibernate-mappings set to default-cascade="save-update" and at first I thought it worked. But when I looked closer, it had saved the new parent, but moved everything from the old parent over to the new one! It looks like I'm going to have to save the parent with no cascade, then get the new parent ID, and set the parent ID on every child before I save it, using a similar method all the way through the graph of objects.
Without the serialization, I've done it that way before, keeping hash tables of old vs. new objects or object IDs. But it involves a lot of opening and closing sessions, keeping track of detached (old) and transient (ancestor) objects, etc. It's prone to error when I add a new table or column to the database. I can do that if I have to, but I thought I'd ask here if anyone has done this before and knows of an easier way?
Any thoughts, suggestions, or advice would be appreciated.
|