greendao存储嵌套数据(二)

2021-08-07  本文已影响0人  重新起步

greendao存储嵌套数据(一)
greendao存储嵌套数据(二)

ABean中包含ListBBean

@Entity
public class ABean {

 private int first;


@Convert(converter = ListBBeanConvert.class, columnType = String.class)
private List bBeans;

@Generated(hash = 1883429528)
 public ABean() {
}

@Generated(hash = 1721348249)
public ABean(int first, List bBeans) {
this.first = first;
 this.bBeans = bBeans;
}

public int getFirst() {
return this.first;
 }

public void setFirst(int first) {
this.first = first;
}

public List getBBeans() {
return this.bBeans;
 }

public void setBBeans(List bBeans) {
 this.bBeans = bBeans;
 }
}

BBean显示如下:

@Entity
public class BBean {

 private int second;

@Generated(hash = 244458771)
public BBean() {
}

@Generated(hash = 410288234)
public BBean(int second) {
this.second = second;
 }

public int getSecond() {
return this.second;
}

 public void setSecond(int second) {
 this.second = second;
}
}

如何把List转换String?

public class ListBBeanConvert implements PropertyConverter, String> {

 @Override
 public List convertToEntityProperty(String databaseValue) {
TypeToken> typeToken = new TypeToken>(){};
return JSONUtil.fromJson(databaseValue, typeToken.getType());
 }

@Override
 public String convertToDatabaseValue(List entityProperty) {
 return JSONUtil.toJson(entityProperty);
}
}

插入代码如下:

List bBeans = new ArrayList<>();
for (int i = 0; i < 5; i++) {
 BBean bBean = new BBean(i);
bBeans.add(bBean);
}

 ABean aBean = new ABean();
aBean.setBBeans(bBeans);
aBean.setFirst(1);

DaoSession daoSession = CalendarSQLiteManager.getInstance().getDaoSession();
 daoSession.insert(aBean);

数据库结果如下:


上一篇下一篇

猜你喜欢

热点阅读