Inserting object data

Hi all,

I’m considering how to best use my data model to insert an object graph into an empty database.  A couple of issues that I want to cover:

  • My data model has heavy inheritance with a combination of joined and mapped subclasses.  Because Hibernate is the final arbiter of storage in this case, I'd like to be able to throw objects at Hibernate and let it work out diffusing of the data into the respective tables. In this manner, if I change the inheritance layout, the same objects will automatically save in the then-current layout.
  • Because this agile layout will have different tables, it will likely generate different keys, breaking hardcoded loads.

In the past, I’ve used XStream to serialize XML that should make up the initial data, then iterate over the deserialized list that it gave me back at load time, saving those objects with Hibernate using cascade for associations.  I could imagine LB having a loadObject element that accepted the same XML (as CDATA) as a part of the changelog.  But I took a look at this and gave up after a few hours of banging on it, getting Hibernate working under LB didn’t seem very clean at all.

Am I overthinking the problem?  What’s the best way to store an object with a complex (and possibly fluid during development) mapping structure like this?

Thanks, Brian

(sorry for the slow delay, I’m very behind)

Managing loading data is a difficult problem, and I don’t think you’re overthinking it.  You are right that getting liquibase and hibernate to work concurrently is difficult because hibernate expects the loading database structure to match what the mapping says it is, while liquibase’s job is to get you to that mapping.  That normally works just fine, unless you try to spin up hibernate before liquibase has fully updated your database.

My general idea has been that even with a fluid database schema, it is best to lock your data loading into the particular schema that you have at the time, and then let the later liquibase steps move the data around like the production data would be.  So, for example, you have a changeSet that creates a table and then a change that populates the data with the original schema.  Then later, you use or other changes to migrate the table as your object hierarchy changes and the original loaded data will be migrated along with the schema.  Trying to manage your data to import separately  from the changes to the data is too easy to mess up.