I am using Android with ORMLite in a small app I am currently writing. The app aims to have a working Import/Export function, for which I use Simple XML framework. And everyth
ORMLite supports a allowGeneratedIdInsert=true
option to @DatabaseField
annotation that allows inserting of an object with the ID already set into a generated-id table. If the value of the ID field is null or default value (0, ...) then the database will generate the ID. This is not supported by all database types (Derby for example). Here's another discussion about this specific topic.
I do think the proper thing to do here is to build your object graph in memory, associating the proper Customer
on their Order
objects before saving an of them to disk. If you read your Customers into memory, then read in the Order
objects and set the real Customer
object on each one. When you then create each Customer
object in the database, ORMLite will change the id
field to the generated one which would change it on the customer_id
field saved in each Order
as well.
If you have a ton of data and can't read it all into memory at one (or for some other reason) then you could always build a Map<Integer,Integer>
and save the Customer
id from the XML mapped to the id you get after you create it in the database. Then when you load in the Order
objects you can set the new corrected id on the foreign object.
Hope this helps. Let me know some more details about how you are reading in the objects and I can give a better example of how to build the object graph.