GSON到Ormlite外国收藏

CQM*_*CQM 5 java serialization gson ormlite android-sqlite

我的模型对象具有简单的数据类型以及其他类型的模型对象

@SerializedName("account")
Account account;`
Run Code Online (Sandbox Code Playgroud)

我使用网络api调用序列化这些模型对象GSON.GSON需要我的模型对象是它们各自的类型.我想使用Ormlite和批处理任务将我的模型对象写入数据库,但Ormlite要求ForeignCollection<T>即使我的模型中只有一个外来对象(而不是外来对象列表).

我如何从一个序列化元素让TForeignCollection<T>在我的模型文件?

真正的问题当然是如何在我的数据库中序列化和存储模型,其中包含对其他模型的引用.

rpa*_*pax 6

你在这里要问的不是很清楚,但也许这可能有用:

assignEmptyForeignCollection

void assignEmptyForeignCollection(T parent,
                                  String fieldName)
                                  throws SQLException
Run Code Online (Sandbox Code Playgroud)

创建一个空集合并将其分配给父对象中的相应字段.这允许您从一开始就向集合中添加内容.例如,假设您有一个具有以下字段的帐户:

 @ForeignCollectionField(columnName = "orders")
 Collection<Order> orders;
Run Code Online (Sandbox Code Playgroud)

然后你会打电话:(在这种情况下)

accoundDao.assignEmptyForeignCollection(account, "orders");
Order order1= new Gson().fromJson(someMethodThatCallsYourNetworkApi(),Order.class);

 // this would add it the collection and the internal DAO
 account.orders.add(order1);
Run Code Online (Sandbox Code Playgroud)