Android-Room数据库(介绍)
前言
在SQLite数据库中,我们可以指定对象之间的关系,因此我们可以将一个或多个对象与一个或多个其他对象绑定。这就是所谓的一对多和多对多的关系。
既然要多表查询,所以表之间就得有关联。这时候我们就得使用新的[email protected]
接下来的内容,就需要上节的内容了
@Entity
public class Company {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private int age;
private String address;
private double salary;
public Company(String name, int age, String address, double salary) {
this.name = name;
this.age = age;
this.address = address;
this.salary = salary;
}
//省略了getter/setter方法
}
下面我们再新建一个与之关联的表
@Entity(foreignKeys = @ForeignKey(entity = Company.class,parentColumns = "id",childColumns = "emp_id",onDelete = CASCADE),
indices = @Index(value={"emp_id"},unique = true))
public class Department {
@PrimaryKey(autoGenerate = true)
private int id;
private String dept;
@ColumnInfo(name = "emp_id")
private int empId;
public Department(String dept, int emp