android - GreenDao 按列名设置实体属性

标签 android greendao

我有一个具有 500 个属性的实体,每个属性都有人类可修改的名称,例如“serialNumber”,但它的 column_name 是“E_A_XNF”。

现在我有一个表单,它为我提供了一个以列名作为键的 Map。

我怎样才能使用 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

如何用这张 map 填充实体?因为该实体仅包含真实姓名列。

最佳答案

你可以做的是使用反射。

//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();

显然这不是性能优化,但您可以根据自己的逻辑自行完成。

关于android - GreenDao 按列名设置实体属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46827101/

相关文章:

java - Green Dao 无法初始化 DAOConfig

java - 实例化 GreenDao 时出现错误 freemarker

android - com.google.android.gms.maps.MapFragment : Cannot resolve symbol "maps"

Android 和 Kotlin 协程 : inappropriate blocking method call

android - EditText 不接受数字输入

android - 如何在 android 的库模块中使用主项目依赖项

android - 使用 Green-DAO 持久化双向实体

java - 如何在 GreenDao 中更新为 "UPDATE table SET col1=' val 1'"

java - green dao的string属性可以存储的字符串最大长度

java - 为什么没有检测到 JAVA 版本? Android Gradle 插件需要 Java 11 才能运行。您当前使用的是 Java 1.8