首页 > 解决方案 > 如何为 Room 数据库的 @Entity 的字段编写循环

问题描述

我正在SQLite为我的本地数据库使用 Room 包装器库,其中一个实体类包括表的文件。

@Entity(tableName = DBContract.PatientDataEntry.TABLE_NAME)
public class PatientRecordEntity {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = DBContract.PatientDataEntry._PID)
public int pid;
@ColumnInfo(name = DBContract.PatientDataEntry.COLUMN_PATIENT_ID)
public String patient_db_ID = "";
@ColumnInfo(name = DBContract.PatientDataEntry.COLUMN_PATIENT_RACE)
public String patient_race = "";
@ColumnInfo(name = DBContract.PatientDataEntry.COLUMN_PATIENT_REAL_IC)
public String patient_IC = "";
@ColumnInfo(name = DBContract.PatientDataEntry.COLUMN_BED_NO)
public String bed_number = "";
@ColumnInfo(name = DBContract.PatientDataEntry.COLUMN_REAL_BED_NO)
public String real_bed_number = "";
@ColumnInfo(name = DBContract.PatientDataEntry.COLUMN_NO_WOUNDS)
public int no_wounds = -1;

我为此查询编写了一个测试,并将 equal 方法添加到存储在该数据库中的对象的campare

public boolean equals(Object o) {
// If the object is compared with itself then return true
if (o == this) 
    return true;

/* Check if o is an instance of PatientRecordEntity or not
"null instanceof [type]" also returns false */
if (!(o instanceof PatientRecordEntity)) 
     return false;


// typecast o to Complex so that we can compare data members
PatientRecordEntity c = (PatientRecordEntity) o;

// Compare the data members and return accordingly
return Double.compare(pid, c.pid) == 0
       && patient_db_ID.equals(c.patient_db_ID)
       && bed_number.equals(c.bed_number)
       && patient_race.equals(c.patient_race)
       && real_bed_number.equals(c.real_bed_number)
       && register_date.equals(c.register_date)
       && register_time.equals(c.register_time)
       && Double.compare(patient_age,c.patient_age)==0
       && Double.compare(patient_gender,c.patient_gender)==0
       && patient_IC.equals(c.patient_IC)
       && Double.compare(no_wounds,c.no_wounds)==0;
}

这是我在 android 测试包中的测试:

@Test
public void addNewPatient() throws Exception {
     PatientRecordEntity newPatient = new PatientRecordEntity();
     newPatient.setPatient_db_ID("123456");
     newPatient.setPatient_race("chines");
     newPatient.setBed_number("123");
     newPatient.setPatient_age(12);

     int newRowId = 0;
     newRowId = (int) patientDao.addNewPatient(newPatient);

     expected_current_patient_record_entity = newPatient;
     actual_current_patient_record_entity = patientDao.getPatientRecord("123456", "123");

     if (expected_current_patient_record_entity.equals(actual_current_patient_record_entity)) {
        assertTrue(true);
     }else {
        System.out.println("Not Equal");
        assertTrue("The getPatientRecord test failed", false);
     }

当两个对象不相等时,我想在测试或相等方法中编写一个循环,给我一个错误,即这些对象的哪些字段不相等

标签: javaandroid

解决方案


我写了硬编码的方法。我声明一个布尔值来检查equals函数内部的相等性

public boolean equals(Object o) {
 // If the object is compared with itself then return true
  if (o == this) 
    return true;

/* Check if o is an instance of PatientRecordEntity or not
 "null instanceof [type]" also returns false */
  if (!(o instanceof PatientRecordEntity)) 
     return false;


// typecast o to Complex so that we can compare data members
PatientRecordEntity c = (PatientRecordEntity) o;


 boolean isEqual = true; //I assume these are equal
// Compare the data members and return accordingly
if (Double.compare(pid, c.pid) != 0) {
  System.out.println("PID not Equal");
  isEqual = false;
}
if (!patient_db_ID.equals(c.patient_db_ID)) {
  System.out.println("Patient ID not Equal");
  isEqual = false;
} 
if (!bed_number.equals(c.bed_number)) {
  System.out.println("Bed number not Equal");
  isEqual = false;
} 
if (!patient_race.equals(c.patient_race)) {
  System.out.println("Patient Race not Equal");
  isEqual = false;
}  
if (!real_bed_number.equals(c.real_bed_number)) {
  System.out.println("Real Bed number not Equal");
  isEqual = false;
} 
if (!register_date.equals(c.register_date)) {
  System.out.println("Registger bed not Equal");
  isEqual = false;
} 
if (!register_time.equals(c.register_time)) {
  System.out.println("Register time not Equal");
  isEqual = false;
} 
if (Double.compare(patient_age,c.patient_age) != 0) {
  System.out.println("Patient age not Equal");
  isEqual = false;
} 
if (Double.compare(patient_gender,c.patient_gender) != 0) {
  System.out.println("Patient gender not Equal");
  isEqual = false;
} 
if (!patient_IC.equals(c.patient_IC)) {
  System.out.println("Patient IC not Equal");
  isEqual = false;
} 
if (Double.compare(no_wounds,c.no_wounds) != 0) {
  System.out.println("no wounds not Equal");
  isEqual = false;
} 

return isEqual; }

推荐阅读