How can I fill the foreign collection manually

百般思念 提交于 2019-12-02 13:57:32

问题


I have:

@ForeignCollectionField(eager = false)
private ForeignCollection<Field> fieldCollection;

and I want to fill this collection from data coming from web service because I want to insert this data to my Sqlite database.

I tried to use this:

boolean accessOnPremiseDb = false;
String description;

@ForeignCollectionField(eager = false)
private ForeignCollection<Entity> entitiyCollection =
     new LazyForeignCollection<Entity, Integer>(null, accessOnPremiseDb, 
          accessOnPremiseDb, null, description, accessOnPremiseDb);

but I got the error

Caused by: java.lang.IllegalStateException: Internal DAO object is null.
   Lazy collections cannot be used if they have been deserialized.

How can I do that without problem? Should I make new variable for this?


回答1:


... I want to fill this collection from data coming from web service because I want to insert this data to my Sqlite database.

Right. The serialization error message is misleading. You are trying to create a foreign collection and you can't call the constructor directly. You should instead be calling

dao.assignEmptyForeignCollection(data, "entitiyCollection");

or

data.entitiyCollection = dao.getEmptyForeignCollection("entitiyCollection");

This wires the appropriate DAO object into the foreign collection. I've improved the javadocs and the error message.

https://github.com/j256/ormlite-core/commit/b4037999c21f45c426ce7a83bc759e3ec8335c61



来源:https://stackoverflow.com/questions/20802976/how-can-i-fill-the-foreign-collection-manually

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