android - ActiveAndroid 与 Cascade 的一对多关系
问题描述
我对如何创建两个与条件相关的表Model
有点困惑,并且找不到任何好的/清晰的例子来学习。所以我有这张桌子:ActiveAndroid
Cascade
onDelete
@Table(name = "CheckList")
public class CheckList {
@Column(name = "Title")
String Title;
@Column(name = "After")
Integer After;
@Column(name = "Before")
Integer Before;
@Column(name = "Enabled")
Boolean Enabled;
@Column(name = "Info")
String Info;
}
我需要在此表中列出它:
@Table(name = "Equipment")
public class Equipment {
@Column(name = "Title")
String Title;
@Column(name = "Checklists")
List<CheckList> Checklists;
}
我也可能有另一个表,其中包含列表,Equipment
我需要像上面一样将它们关联起来。
我想要的是,当我从中删除记录时Equipment
,我需要删除List<CheckList> Checklists;
与此相关的所有记录Equipment
。我知道我可以进行查询等,但我需要知道是否有更好的方法和正确的方法来做到这一点?
请详细解释(如何稍后创建关系和查询)并显示与我的表相关的示例。
解决方案
您需要设置具有外键级联关系的表。
@Table(name = "Equipment")
public class Equipment {
@Column(name = "Title")
String Title;
// This method is optional & does not affect the foreign key creation.
public List<CheckList> items() {
return getMany(CheckList.class, "Equipment");
}
}
@Table(name = "CheckList")
public class CheckList {
@Column(name = "Title")
String Title;
@Column(name = "After")
Integer After;
@Column(name = "Before")
Integer Before;
@Column(name = "Enabled")
Boolean Enabled;
@Column(name = "Info")
String Info;
//This establishes a relationship between Checklist and Equipment, Any update or delete operations done on Equipment table gets cascaded to the corresponding rows in the Checklist table.
@Column(name = "Equipment", onUpdate = ForeignKeyAction.CASCADE, onDelete = ForeignKeyAction.CASCADE)
Equipment equipment;
}
参考资料:
推荐阅读
- python - 如何从具有单列的文本文件创建数据框
- excel - 如何在 VBA excel 宏中实现单元格选择
- rust - 如何让struct持有一个线程并在它超出范围后立即销毁线程
- java - 在使用 switch 语句的文本菜单上更改输出
- javascript - 全部完成后的 Javascript 回调
- vue.js - Vue 路由中的 beforeEnter 不适用于子路由
- routes - IONIC 4 - 路由导航
- android - Android ARM C++异常调试
- javascript - 手机中的按下行为 Ionic / Angular / Cordova App
- python - 两个文件的区别