简体   繁体   English

GreenDao通过列名设置实体属性

[英]GreenDao set entity property by column name

I have an entity with 500 properties, each of those have human redeable names like 'serialNumber', but it's column_name is 'E_A_XNF'. 我有一个具有500个属性的实体,每个属性都具有可人工重命名的名称,例如'serialNumber',但column_name为'E_A_XNF'。

Now I have a form which gives me a Map with the column name as a key. 现在,我有了一个表单,该表单为我提供了一个以列名称为键的Map。

How can I, with GreenDao, do something like : 使用GreenDao,我该怎么做:

String columnName = 'E_A_XNF';
String serialNumber = myMap.get(columnName);
entity.setByColumnName(columnName, serialNumber);

obviously in the real program I will loop over every entry in the map. 显然,在实际程序中,我将遍历地图中的每个条目。

Thanks. 谢谢。

EDIT : 编辑:

I have this entity : 我有这个实体:

@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

    }

then I have this : 然后我有这个:

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

How do I fill the entity with this map? 如何用此地图填充实体? since the entity only contains the column real names. 因为实体仅包含列实名。

What you can do is using the reflection. 您可以做的是使用反射。

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

Obviously this is not performance optimized but you can do it yourself based on your logic. 显然,这不是性能优化的,但是您可以根据自己的逻辑进行操作。

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

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