Java Android Room外键onDelte=级联无效
我在两个表之间有一对多关系。父表是“人”,子表是“宠物”。一个人可以养很多宠物。一只宠物只能有一个人 我使用@relation在PersonWithPets.java中创建两个表之间的关系。在Pets.java中,我使用ForeignKey和onDelete=CASCADE。但是如果我删除一个人,相应的宠物会保留在数据库中,我不知道为什么。我几乎觉得onDelte=CASCADE没有效果。 也许我没有使用正确的身份证,但我已经过了很多次了,只是似乎不明白为什么一个人的宠物不会被这个人删除 当然,我可以使用宠物的personId来迭代和删除所有共享同一personId的宠物,但这似乎违背了@ForeignKey注释的目的 我会错过什么? 对不起,如果这是显而易见的。我是Android新手 Person.javaJava Android Room外键onDelte=级联无效,java,android,foreign-keys,android-room,cascade,Java,Android,Foreign Keys,Android Room,Cascade,我在两个表之间有一对多关系。父表是“人”,子表是“宠物”。一个人可以养很多宠物。一只宠物只能有一个人 我使用@relation在PersonWithPets.java中创建两个表之间的关系。在Pets.java中,我使用ForeignKey和onDelete=CASCADE。但是如果我删除一个人,相应的宠物会保留在数据库中,我不知道为什么。我几乎觉得onDelte=CASCADE没有效果。 也许我没有使用正确的身份证,但我已经过了很多次了,只是似乎不明白为什么一个人的宠物不会被这个人删除 当然,
@Entity(tableName = "person")
public class Person {
@PrimaryKey(autoGenerate = true)
private Integer id;
String name;
@Ignore
private List<Pet> pets = null;
public Person(){
// empty constructor for serialization
}
@Ignore
public Person(String name, List<Pet> pets) {
this.name = name;
this.pets = pets;
}
public Person(PersonWithPets personWithPets) {
this.id = personWithPets.getPerson().getId();
this.name = personWithPets.getPerson().getName();
this.pets = this.getPets(personWithPets);
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setPet(Pet pet) {
if (this.pets == null) {
this.pets = new ArrayList<>();
}
this.pets.add(pet);
}
public List<Pet> getPets() {
return this.pets;
}
private List<Pet> getPets(PersonWithPets personWithPets) {
return personWithPets.getPets();
}
public void setName(String name) {
this.name = name;
}
}
@Entity(tableName = "pet")
public class Pet {
@PrimaryKey(autoGenerate = true)
private Integer id;
String name;
public Pet() {
}
/**
* @param name
*/
@Ignore
public Pet(String name) {
this.name = name;
}
@ForeignKey
(entity = Person.class,
parentColumns = "id",
childColumns = "personId",
onDelete = CASCADE,
onUpdate = CASCADE)
private Integer personId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
}
public class PersonWithPets {
@Embedded public Person person;
@Relation(
parentColumn = "id",
entityColumn = "personId",
entity = Pet.class
)
public List<Pet> pets;
public Person getPerson() {
return person;
}
public List<Pet> getPets() {
return pets;
}
}
PersonWithPets.java
@Entity(tableName = "person")
public class Person {
@PrimaryKey(autoGenerate = true)
private Integer id;
String name;
@Ignore
private List<Pet> pets = null;
public Person(){
// empty constructor for serialization
}
@Ignore
public Person(String name, List<Pet> pets) {
this.name = name;
this.pets = pets;
}
public Person(PersonWithPets personWithPets) {
this.id = personWithPets.getPerson().getId();
this.name = personWithPets.getPerson().getName();
this.pets = this.getPets(personWithPets);
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setPet(Pet pet) {
if (this.pets == null) {
this.pets = new ArrayList<>();
}
this.pets.add(pet);
}
public List<Pet> getPets() {
return this.pets;
}
private List<Pet> getPets(PersonWithPets personWithPets) {
return personWithPets.getPets();
}
public void setName(String name) {
this.name = name;
}
}
@Entity(tableName = "pet")
public class Pet {
@PrimaryKey(autoGenerate = true)
private Integer id;
String name;
public Pet() {
}
/**
* @param name
*/
@Ignore
public Pet(String name) {
this.name = name;
}
@ForeignKey
(entity = Person.class,
parentColumns = "id",
childColumns = "personId",
onDelete = CASCADE,
onUpdate = CASCADE)
private Integer personId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
}
public class PersonWithPets {
@Embedded public Person person;
@Relation(
parentColumn = "id",
entityColumn = "personId",
entity = Pet.class
)
public List<Pet> pets;
public Person getPerson() {
return person;
}
public List<Pet> getPets() {
return pets;
}
}
public class PersonWithPets{
@嵌入式公众人物;
@关系(
parentColumn=“id”,
entityColumn=“personId”,
实体=Pet.class
)
公开宠物名单;
公众人物{
返回人;
}
公共列表getPets(){
归还宠物;
}
}
PersonRepository
public class PersonRepository {
private static final String TAG = "RecipeRepository";
private PersonDao personDao;
private LiveData<List<Person>> people;
private LiveData<List<PersonWithPets>> peopleWithPets;
private LiveData<List<Pet>> pets;
public PersonRepository (Application application) {
PersonDatabase db = PersonDatabase.getInstance(application);
personDao = db.personDao();
people = personDao.getAllPeople();
peopleWithPets = personDao.getAllPeopleWithPets();
pets = personDao.getAllPets();
}
// Get all people with pets
public LiveData<List<PersonWithPets>> getAllPeopleWithPets() {
return peopleWithPets;
}
// Delete
public void delete(Person person) {
new DeletePersonAsyncTask(personDao).execute(person);
}
// Delete All People
private static class DeleteAllPeopleAsyncTask extends AsyncTask<Void, Void, Void> {
private PersonDao personDao;
private DeleteAllPeopleAsyncTask(PersonDao personDao) {
this.personDao = personDao;
}
@Override
protected Void doInBackground(Void... voids) {
personDao.deleteAllPeople();
//personDao.deleteAllPets();
return null;
}
}
}
viewModel.getAllPeopleWithPets().observe(this, new Observer<List<PersonWithPets>>() {
@Override
public void onChanged(@Nullable List<PersonWithPets> peopleWithPets) {
Log.d(TAG, "onChanged: --------------------------------");
Log.d(TAG, "onChanged: ALL PEOPLE WITH PETS");
for (PersonWithPets personWithPets : peopleWithPets) {
Log.d(TAG, "onChanged: Name: " + personWithPets.getPerson().getName() + " ID: " + personWithPets.getPerson().getId());
Log.d(TAG, "onChanged: Pets: " + + personWithPets.getPets().size());
if (personWithPets.getPets().size() > 0) {
for (Pet pet : personWithPets.getPets()) {
Log.d(TAG, "onChanged: Name: " + pet.getName() + " ID: " + pet.getId() + " PersonID: " + pet.getPersonId());
}
} else {
Log.d(TAG, "onChanged: No pets.");
}
}
Log.d(TAG, "onChanged: --------------------------------");
setTitle("People: " + peopleWithPets.size() + " Pets: " + viewModel.getAllPets().getValue().size());
}
});
公共类PersonRepository{
私有静态最终字符串TAG=“RecipeRepository”;
私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人;
私人LiveData人;
有宠物的私人生活资料;
私人LiveData宠物;
公共PersonRepository(应用程序){
PersonDatabase db=PersonDatabase.getInstance(应用程序);
personDao=db.personDao();
people=personDao.getAllPeople();
peopleWithPets=personDao.getAllPeopleWithPets();
pets=personDao.getAllPets();
}
//让所有人都养宠物
public LiveData getAllPeopleWithPets(){
让人们带上宠物;
}
//删除
公共作废删除(人){
新建DeletePersonAsyncTask(personDao)。执行(person);
}
//删除所有人
私有静态类deleteAllPeopleSyncTask扩展异步任务{
私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人;
私有deleteAllPeopleSyncTask(PersonDao PersonDao){
this.personDao=personDao;
}
@凌驾
受保护的空位背景(空位…空位){
personDao.deleteAllPeople();
//personDao.deleteAllPets();
返回null;
}
}
}
数据库
@Database(entities = {Person.class, Pet.class}, version = 5, exportSchema = false)
public abstract class PersonDatabase extends RoomDatabase {
private static final String TAG = "PersonDatabase";
private static PersonDatabase instance;
public abstract PersonDao personDao();
public static synchronized PersonDatabase getInstance(Context context) {
if(instance == null){
instance = Room.databaseBuilder(context.getApplicationContext(),
PersonDatabase.class, "person_database")
.fallbackToDestructiveMigration()
.addCallback(roomCallback)
.build();
}
return instance;
}
private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback(){
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
new PopulateDbAsyncTask(instance).execute();
}
};
private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> {
private PersonDao personDao;
private PopulateDbAsyncTask(PersonDatabase db){
personDao = db.personDao();
}
@Override
protected Void doInBackground(Void... voids) {
Person adam = new Person();
adam.setName("Adam");
Pet dog = new Pet();
dog.setName("Dog");
adam.setPet(dog);
Long personId = personDao.insertPerson(adam);
for (Pet pet : adam.getPets()) {
pet.setPersonId(personId.intValue());
personDao.insertPet(pet);
}
return null;
}
}
}
@数据库(实体={Person.class,Pet.class},版本=5,exportSchema=false)
公共抽象类PersonDatabase扩展了RoomDatabase{
私有静态最终字符串TAG=“PersonDatabase”;
私有静态PersonDatabase实例;
公开摘要PersonDao PersonDao();
公共静态同步PersonDatabase getInstance(上下文){
if(实例==null){
实例=Room.databaseBuilder(context.getApplicationContext(),
PersonDatabase.class,“个人数据库”)
.fallbackToDestructiveMigration()
.addCallback(roomCallback)
.build();
}
返回实例;
}
私有静态RoomDatabase.Callback roomCallback=新建RoomDatabase.Callback(){
@凌驾
public void onCreate(@NonNull SupportSQLiteDatabase db){
super.onCreate(db);
新填充的Basynctask(实例).execute();
}
};
私有静态类填充Basynctask扩展异步任务{
私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人私人;
私有填充的Basynctask(PersonDatabase db){
personDao=db.personDao();
}
@凌驾
受保护的空位背景(空位…空位){
人物亚当=新人();
adam.setName(“亚当”);
宠物狗=新宠物();
dog.setName(“dog”);
亚当。赛特宠物(狗);
Long personId=personDao.insertPerson(亚当);
for(宠物:adam.getPets()){
pet.setPersonId(personId.intValue());
personDao.insertPet(pet);
}
返回null;
}
}
}
DAO
@Dao
public interface PersonDao {
@Transaction
@Insert
Long insertPerson(Person person);
@Transaction
@Insert
void insertPet(Pet pet);
@Transaction
@Update
int updatePerson(Person person);
@Transaction
@Delete
void deletePerson(Person person);
@Transaction
@Query("DELETE FROM person")
void deleteAllPeople();
@Transaction
@Query("DELETE FROM pet")
void deleteAllPets();
@Transaction
@Query("SELECT * FROM person")
LiveData<List<Person>> getAllPeople();
@Transaction
@Query("SELECT * FROM person")
LiveData<List<PersonWithPets>> getAllPeopleWithPets();
@Transaction
@Query("SELECT * FROM pet")
LiveData<List<Pet>> getAllPets();
}
@Dao
公共接口PersonDao{
@交易
@插入
长插入人(人);
@交易
@插入
void-insertPet(Pet-Pet);
@交易
@更新
int updatePerson(个人);
@交易
@删除
无效删除人(人);
@交易
@查询(“从个人删除”)
void deleteAllPeople();
@交易
@查询(“从宠物中删除”)
void deleteAllPets();
@交易
@查询(“从人员中选择*)
LiveData getAllPeople();
@交易
@查询(“从人员中选择*)
LiveData获取所有带宠物的人();
@交易
@查询(“从宠物中选择*)
LiveData getAllPets();
}
main活动
public class PersonRepository {
private static final String TAG = "RecipeRepository";
private PersonDao personDao;
private LiveData<List<Person>> people;
private LiveData<List<PersonWithPets>> peopleWithPets;
private LiveData<List<Pet>> pets;
public PersonRepository (Application application) {
PersonDatabase db = PersonDatabase.getInstance(application);
personDao = db.personDao();
people = personDao.getAllPeople();
peopleWithPets = personDao.getAllPeopleWithPets();
pets = personDao.getAllPets();
}
// Get all people with pets
public LiveData<List<PersonWithPets>> getAllPeopleWithPets() {
return peopleWithPets;
}
// Delete
public void delete(Person person) {
new DeletePersonAsyncTask(personDao).execute(person);
}
// Delete All People
private static class DeleteAllPeopleAsyncTask extends AsyncTask<Void, Void, Void> {
private PersonDao personDao;
private DeleteAllPeopleAsyncTask(PersonDao personDao) {
this.personDao = personDao;
}
@Override
protected Void doInBackground(Void... voids) {
personDao.deleteAllPeople();
//personDao.deleteAllPets();
return null;
}
}
}
viewModel.getAllPeopleWithPets().observe(this, new Observer<List<PersonWithPets>>() {
@Override
public void onChanged(@Nullable List<PersonWithPets> peopleWithPets) {
Log.d(TAG, "onChanged: --------------------------------");
Log.d(TAG, "onChanged: ALL PEOPLE WITH PETS");
for (PersonWithPets personWithPets : peopleWithPets) {
Log.d(TAG, "onChanged: Name: " + personWithPets.getPerson().getName() + " ID: " + personWithPets.getPerson().getId());
Log.d(TAG, "onChanged: Pets: " + + personWithPets.getPets().size());
if (personWithPets.getPets().size() > 0) {
for (Pet pet : personWithPets.getPets()) {
Log.d(TAG, "onChanged: Name: " + pet.getName() + " ID: " + pet.getId() + " PersonID: " + pet.getPersonId());
}
} else {
Log.d(TAG, "onChanged: No pets.");
}
}
Log.d(TAG, "onChanged: --------------------------------");
setTitle("People: " + peopleWithPets.size() + " Pets: " + viewModel.getAllPets().getValue().size());
}
});
viewModel.getAllPeopleWithPets().observe(这是一个新的观察者(){
@凌驾
更改后的公共无效(@Nullable List peopleWithPets){
Log.d(标记“onChanged:------------------------------------”;
Log.d(标签“onChanged:ALL PEOPLE WITH PETS”);
for(带宠物的人带宠物的人:带宠物的人){
Log.d(标记“onChanged:Name:”+personWithPets.getPerson().getName()+“ID:”+personWithPets.getPerson().getId());
Log.d(标记“onChanged:Pets:++personWithPets.getPets().size());
如果(personWithPets.getPets().size()>0){
for(宠物:personWithPets.getPets()){
Log.d(标记“onChanged:Name:”+pet.getName()+“ID:”+pet.getId()+“PersonID