因此,我们有一个项目,其中我们使用会议室数据库开发应用程序。我们想存储不同类型的问题,因此我们为一个通用问题创建了一个实体,其他类型的问题扩展了该通用问题的实体。现在,我们想在子类和父类中插入一个问题,以便可以通过相同的ID查找它们。在两个表中都可以输入问题的最佳方法是什么,以便我们可以链接它们?
@Entity(tableName = TableConstants.TABLE_NAME_QUESTION)
public class Question implements Parcelable
@PrimaryKey(autoGenerate = true)
private int qid;
private String question;
private String answer;
public Question(String question, String answer) {
this.question = question;
this.answer = answer;
}
@Entity(tableName = TableConstants.TABLE_NAME_CHOICE_QUESTION, foreignKeys = @ForeignKey(entity = Question.class,
parentColumns = "qid", childColumns = "mcqid", onDelete = ForeignKey.CASCADE))
public class ChoiceQuestion extends Question {
private int mcqid;
private String mcq;
private String mca;
private String option1;
private String option2;
private String option3;
private String option4;
public ChoiceQuestion (String mcq, String mca, String option1, String option2,
String option3, String option4) {
super(mcq, mca);
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
}