我正在使用GreenDAO和Volley。所以我有以下问题:当我进行网络请求时,我需要使用GSON解析,以便我有一个模型来表示从服务器检索到的实体和另一个模型来表示GreenDAO对象。是否有任何办法只拥有每个模型的1个类,以表示为GSON和ORM的Class?
类Product:
@SerializedName("id")
private String id;
@SerializedName("pictures")
private List<Picture> pictures;
get & set
持久化产品类:
private Long id;
private List<Picture> pictures;
/** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
public List<PersistencePicture> getPictures() {
if (pictures == null) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
PersistencePictureDao targetDao = daoSession.getPersistencePictureDao();
List<PersistencePicture> picturesNew = targetDao._queryPersistenceProduct_Pictures(id);
synchronized (this) {
if(pictures == null) {
pictures = picturesNew;
}
}
}
return pictures;
}
一开始我想创建一个接口,但是当你从DAO检索数据时,DAO返回的是类而不是接口,因此我认为无法以这种方式实现。我找到的唯一解决方案是创建一个“ProductUtils”,将“PersistentProduct”转换为“Product”,反之亦然。