WillFlow 2019-06-27
现有一个实体类Card(证件)
其中有属性:id、证件名称、姓名、卡号、备注,以及证件的照片,其中证件的照片可能有多张(1到4张),故需要用到一对多的关系。
证件类:Card
照片类:Photo
GreenDao语法:
@ToMany(referencedJoinProperty = "parentId")
//实体类:证件 Card
@Entity
public class Card {
//ID主键自增
@Id(autoincrement = true)
private Long id;
@NotNull
private String card_name;//证件名称
@NotNull
private String username;//证件人姓名
@NotNull
private String card_number;//卡号
private String remark;//备注
//cardId为Photo类中定义的一个属性
@ToMany(referencedJoinProperty = "cardId")
private List<Photo> photoList;
}//实体类Photo
@Entity
public class Photo {
//ID主键自增
@Id(autoincrement = true)
private Long id;
@NotNull
private String photo_path;//证件照片路径
@NotNull
private Long cardId;//设计此字段,然后在Card中引用它,以便于Card来识别它
}private void AddRecord() {
String cardname = et_card_name.getText().toString();
String username = et_user_name.getText().toString();
String cardnumber = et_card_number.getText().toString();
String remark = et_remark.getText().toString();
Card card = new Card(cardname, username, cardnumber, remark);
cardDao.insert(card);
//count_photo为图片的总数
//photoPathList为图片的绝对路径的List集合
for (int i = 0; i < count_photo; i++) {
Photo photo = new Photo();
photo.setCardId(card.getId());
photo.setPhoto_path(photoPathList.get(i));
photoDao.insert(photo);
}
Toasty.success(context,"保存完毕").show();
}

Photo对应了相同的Card1、Card表:

2、Photo表:
