Can I temporarily suspend auto-generated ID in ORMLite?

拥有回忆 提交于 2019-12-02 08:11:14
Gray

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!