有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何手动填充外部集合

我有:

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

我想从来自web服务的数据中填充此集合,因为我想将此数据插入我的Sqlite数据库

我试着用这个:

boolean accessOnPremiseDb = false;
String description;

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

但是我弄错了

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

我怎样才能毫无问题地做到这一点?我应该为此创建新变量吗


共 (1) 个答案

  1. # 1 楼答案

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

    对。序列化错误消息具有误导性。您正在尝试创建外部集合,无法直接调用构造函数。你应该打电话

    dao.assignEmptyForeignCollection(data, "entitiyCollection");
    

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

    这会将适当的DAO对象连接到外部集合中。我改进了javadocs和错误消息

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