kai*_*uki 7 database android entity-relationship one-to-many android-room
我正在尝试建立一对多关系,我在线检查了各种文章和教程,但所有示例都显示一个表与另一个表具有一对多关系。
在我的要求中,我有两个表与另一个表具有一对多关系(检查下图)
学生表有来自班级和学校表的 FK。大多数示例通过班级 - 学生或学校 - 学生解释一对多关系。
我想做[班级,学校] - 学生。
Core Data 在 iOS 上处理这个问题,但我无法在 Android 上解决这个问题。我什至尝试了 Android 文档中显示的嵌套关系示例,但这不起作用。
Mik*_*keT 17
您的问题尚未明确说明。但是,以下是一个工作示例,演示了根据架构提取关系的两种方法。
示例代码
学校实体:-
@Entity(tableName = "_school")
class School {
@PrimaryKey
@ColumnInfo(name = "school_id")
Long Schoolid;
@NonNull
@ColumnInfo(name = "school_name")
String SchoolName;
School(){}
@Ignore
School(String schoolName) {
this.SchoolName = schoolName;
}
}
Run Code Online (Sandbox Code Playgroud)
班级(名称的不明智选择):-
@Entity(tableName = "_class")
class Class {
@PrimaryKey
@ColumnInfo(name = "class_id")
Long ClassId;
@NonNull
@ColumnInfo(name = "class_name")
String ClassName;
Class(){}
@Ignore
Class(String className) {
this.ClassName = className;
}
}
Run Code Online (Sandbox Code Playgroud)
学生实体(包括外键约束):-
@Entity(
tableName = "_student", foreignKeys = {
@ForeignKey(
entity = School.class,
parentColumns = {"school_id"},
childColumns = {"school_id"},
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
),
@ForeignKey(
entity = Class.class,
parentColumns = {"class_id"},
childColumns = {"class_id"},
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
)
}
)
class Student {
@PrimaryKey
@ColumnInfo(name = "student_id")
Long StudentId;
@ColumnInfo(name = "Student_name")
String StudentName;
@ColumnInfo(name = "school_id", index = true)
Long SchoolId;
@ColumnInfo(name = "class_id", index = true)
Long ClassId;
Student(){}
@Ignore
Student(String studentName, long schoolId, long classId) {
this.StudentName = studentName;
this.SchoolId = schoolId;
this.ClassId = classId;
}
}
Run Code Online (Sandbox Code Playgroud)
POJO 方法 1 - 类StudentAndSchoolAndClass -(不使用@Relation)
class StudentAndSchoolAndClass {
@Embedded
Student student;
String school_name;
String class_name;
}
Run Code Online (Sandbox Code Playgroud)
POJO 方法 2 - 类StudentWithSchoolWithClass - (使用@Relation's)
class StudentWithSchoolWithClass {
@Embedded
Student student;
@Relation(entity = School.class,parentColumn = "school_id", entityColumn = "school_id")
List<School> schoolList;
@Relation(entity = Class.class,parentColumn = "class_id",entityColumn = "class_id")
List<Class> classList;
}
Run Code Online (Sandbox Code Playgroud)
Dao 接口AllDao
@Dao
interface AllDao {
@Insert
Long insertSchool(School s);
@Insert
Long insertClass(Class c);
@Insert
Long insertStudent(Student s);
@Query("SELECT * FROM _school")
List<School> getAllSchools();
@Query("SELECT * FROM _school WHERE school_id = :school_id ")
School getSchoolById(Long school_id);
@Query("SELECT * FROM _class")
List<Class> getAllClasses();
@Query("SELECT * FROM _class WHERE class_id = :class_id")
Class getClassById(Long class_id);
@Query("SELECT * FROM _student JOIN _school ON _school.school_id = _student.school_id JOIN _class ON _class.class_id = _student.class_id")
List<StudentAndSchoolAndClass> getStudentAndSchoolAndClass();
@Query("SELECT * FROM _student")
List<StudentWithSchoolWithClass> getStudentWithSchoolWithClass();
}
Run Code Online (Sandbox Code Playgroud)
@Database 类MyDatabase
@Database(entities = {School.class,Class.class,Student.class},version = 1)
abstract class MyDatabase extends RoomDatabase {
abstract AllDao allDao();
}
Run Code Online (Sandbox Code Playgroud)
最后一个 Activity MainActivity将一些数据加载到数据库中,然后使用 2 个 @Queries 和相应的 POJO 类提取一些数据。
public class MainActivity extends AppCompatActivity {
MyDatabase db;
AllDao allDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Build the MyDatabase instance
db = Room.databaseBuilder(this,MyDatabase.class,"mydb")
.allowMainThreadQueries()
.build();
// Build the allDao instance
allDao = db.allDao();
// Create some school objects
School[] s_array = {new School("School1"),
new School("School2"),
new School("School3")
};
// Insert the Schools into the database
for (School s: s_array) {
allDao.insertSchool(s);
}
// Create some Class objects
Class[] c_array = {
new Class("Class1"),
new Class("Class2"),
new Class("Class3")
};
// Insert the classes
for (Class c: c_array) {
allDao.insertClass(c);
}
// Create some Student Objects
Student[] st_array = {
new Student("Fred",3,3), new Student("Mary",1,2)
};
//Insert the Students
for(Student st: st_array) {
allDao.insertStudent(st);
}
// Get the Students with the School and Class information using POJO 1 (realtionship via joins)
List<StudentAndSchoolAndClass> sasac = allDao.getStudentAndSchoolAndClass();
// Log the data
for(StudentAndSchoolAndClass ssc: sasac) {
Log.d("STUDENTINFO1","Student Name = " + ssc.student.StudentName +
"\n\t ID=" + ssc.student.StudentId + " SchoolID=" + ssc.student.SchoolId + " ClassID=" + ssc.student.ClassId +
"\n\t\t School Name = " + ssc.school_name +
"\n\t\t Class Name = " + ssc.class_name
);
}
// Get the Students with the School and Class information using POJO 2 (with @Relation's)
List<StudentWithSchoolWithClass> swswc = allDao.getStudentWithSchoolWithClass();
for(StudentWithSchoolWithClass ssc: swswc) {
Log.d("STUDENTINFO2","Student Name = " + ssc.student.StudentName +
"\n\t ID=" + ssc.student.StudentId + " SchoolID=" + ssc.student.SchoolId + " ClassID=" + ssc.student.ClassId +
"\n\t\t School Name = " + ssc.schoolList.get(0).SchoolName +
"\n\t\t Class Name = " + ssc.classList.get(0).ClassName
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果
当以上运行(第一次)时,数据库是:-
日志包含:-
2021-04-01 22:09:51.977 D/STUDENTINFO1: Student Name = Fred
ID=1 SchoolID=3 ClassID=3
School Name = School3
Class Name = Class3
2021-04-01 22:09:51.977 D/STUDENTINFO1: Student Name = Mary
ID=2 SchoolID=1 ClassID=2
School Name = School1
Class Name = Class2
2021-04-01 22:09:51.982 D/STUDENTINFO2: Student Name = Fred
ID=1 SchoolID=3 ClassID=3
School Name = School3
Class Name = Class3
2021-04-01 22:09:51.982 D/STUDENTINFO2: Student Name = Mary
ID=2 SchoolID=1 ClassID=2
School Name = School1
Class Name = Class2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7540 次 |
| 最近记录: |