Can I temporarily suspend auto-generated ID in ORMLite?

前端 未结 1 1982
忘掉有多难
忘掉有多难 2021-01-28 02:08

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

相关标签:
1条回答
  • 2021-01-28 03:02

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

    0 讨论(0)
提交回复
热议问题