GreenDao 按列名设置实体 属性
GreenDao set entity property by column name
我有一个具有 500 个属性的实体,每个属性都有人类可重复的名称,例如 'serialNumber',但它的 column_name 是 'E_A_XNF'。
现在我有一个表单,它为我提供了一个以列名作为键的地图。
我如何使用 GreenDao 做类似的事情:
String columnName = 'E_A_XNF';
String serialNumber = myMap.get(columnName);
entity.setByColumnName(columnName, serialNumber);
显然在实际程序中我将遍历映射中的每个条目。
谢谢。
编辑:
我有这个实体:
@Entity(nameInDb = "T_REF_CHAMP", generateConstructors = false,
indexes = {@Index(value = "id", unique = true)})
public class ChampEntity {
@Property(nameInDb = "ID_DAO")
@Id
private Long idDAO;
@Property(nameInDb = "ID_CHAM")
@NotNull
private String id;
@Property(nameInDb = "TAILLE_MAX_CHAM")
private String tailleMaximum;
@Property(nameInDb = "TYPE_CODE_CHAM")
private String typeCode;
@Property(nameInDb = "VAL_DEFAUT_CHAM")
private String defaultValue;
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//getter setter
}
然后我有这个:
Map<String, String> dataToStore = new Hashmap<>();
dataToStore.put(ID_CHAM, "qzdqzddqzd");
dataToStore.put(TAILLE_MAX_CHAM, "122");
dataToStore.put(TYPE_CODE_CHAM, "lolola");
dataToStore.put(VAL_DEFAUT_CHAM, "ergot");
//more than 500 other values
如何用这张地图填充实体?因为实体只包含真实姓名列。
你可以做的是使用反射。
//example class
public class TestEntity {
@Property(nameInDb = "NAME")
public String name;
@Property(nameInDb = "SURNAME")
public String surname;
@Override
public String toString() {
return "TestEntity{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
"}";
}
}
//example map
Map<String, String> dataStore = new HashMap<>();
dataStore.put("NAME", "mat");
dataStore.put("SURNAME", "pag");
//use reflection to fill object
TestEntity te = new TestEntity();
Field[] fields = te.getClass().getFields();
for (Field field : fields) {
//search the field for the GreenDao Property annotation
Property propertyAnnot = field.getAnnotation(Property.class);
if (propertyAnnot != null) {
for (Map.Entry<String, String> pair : dataStore.entrySet()) {
//match the map key and the nameInDb value
if (pair.getKey().equals(propertyAnnot.nameInDb())) {
try {
field.set(te, pair.getValue());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
//check the values of the filled object
te.toString();
显然这不是性能优化,但你可以根据你的逻辑自己做。
我有一个具有 500 个属性的实体,每个属性都有人类可重复的名称,例如 'serialNumber',但它的 column_name 是 'E_A_XNF'。
现在我有一个表单,它为我提供了一个以列名作为键的地图。
我如何使用 GreenDao 做类似的事情:
String columnName = 'E_A_XNF';
String serialNumber = myMap.get(columnName);
entity.setByColumnName(columnName, serialNumber);
显然在实际程序中我将遍历映射中的每个条目。
谢谢。
编辑:
我有这个实体:
@Entity(nameInDb = "T_REF_CHAMP", generateConstructors = false,
indexes = {@Index(value = "id", unique = true)})
public class ChampEntity {
@Property(nameInDb = "ID_DAO")
@Id
private Long idDAO;
@Property(nameInDb = "ID_CHAM")
@NotNull
private String id;
@Property(nameInDb = "TAILLE_MAX_CHAM")
private String tailleMaximum;
@Property(nameInDb = "TYPE_CODE_CHAM")
private String typeCode;
@Property(nameInDb = "VAL_DEFAUT_CHAM")
private String defaultValue;
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//more than 500 other property
//getter setter
}
然后我有这个:
Map<String, String> dataToStore = new Hashmap<>();
dataToStore.put(ID_CHAM, "qzdqzddqzd");
dataToStore.put(TAILLE_MAX_CHAM, "122");
dataToStore.put(TYPE_CODE_CHAM, "lolola");
dataToStore.put(VAL_DEFAUT_CHAM, "ergot");
//more than 500 other values
如何用这张地图填充实体?因为实体只包含真实姓名列。
你可以做的是使用反射。
//example class
public class TestEntity {
@Property(nameInDb = "NAME")
public String name;
@Property(nameInDb = "SURNAME")
public String surname;
@Override
public String toString() {
return "TestEntity{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
"}";
}
}
//example map
Map<String, String> dataStore = new HashMap<>();
dataStore.put("NAME", "mat");
dataStore.put("SURNAME", "pag");
//use reflection to fill object
TestEntity te = new TestEntity();
Field[] fields = te.getClass().getFields();
for (Field field : fields) {
//search the field for the GreenDao Property annotation
Property propertyAnnot = field.getAnnotation(Property.class);
if (propertyAnnot != null) {
for (Map.Entry<String, String> pair : dataStore.entrySet()) {
//match the map key and the nameInDb value
if (pair.getKey().equals(propertyAnnot.nameInDb())) {
try {
field.set(te, pair.getValue());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
//check the values of the filled object
te.toString();
显然这不是性能优化,但你可以根据你的逻辑自己做。