简体   繁体   English

如何为Room数据库的@Entity的字段编写循环

[英]How to write a loop for fields of an @Entity of a Room database

I am using Room SQLite wrapper library for my local database with an entity class includes Fileds of the table. 我正在使用我的本地数据库的Room SQLite包装器库,其中的实体类包括表的Fileds。

@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;

I wrote a test for the queries of this and add equal method to campare of the objects that stored in this database 我为此查询编写了一个测试,并向存储在此数据库中的对象添加了相等的方法

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;
}

and this is my test in android test package: 这是我在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);
     }

I want to write a loop in the test or in an equal method for when two objects aren't equal, gives me an error that which fields of these object aren't equal 我想在测试或相等方法中编写一个循环,用于当两个对象不相等时,给我一个错误,即这些对象的哪些字段不相等

I write the hard coded method. 我写了硬编码的方法。 I declare one boolean to check equality inside equals function 我声明一个布尔值来检查equals函数中的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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM