如何在没有字段的实体上使用Android Room? [英] How to use Android Room on entities without fields?

查看:45
本文介绍了如何在没有字段的实体上使用Android Room?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由HashMap支持的java对象,因此没有可通过反射发现的纯字段.例如:

I have java objects which are are backed by a HashMap and thus do not have plain fields which can be discovered via reflection. For example:

public class City {
    private final HashMap<String, String> internalMap = new HashMap<String, String>();

    public String getId() {
        return internalMap.get("id");
    }

    public void setId(String id) {
        internalMap.put("id", id);
    }

    public String getName() {
        return internalMap.get("name");
    }

    public void setName(String name) {
        internalMap.put("name", name);
    }
}

我想使用这样的类作为Room中的实体,而不必更改其结构,因为我有许多这样的类,这些类是使用代码生成工具自动生成的,并且出于某些特殊原因,它们需要由HashMap支持. HashMap的每个值都应该以我的数据库表中的一列结尾(关键字String是内部实现细节).是否有可能?由于注释处理器是如何发现字段的,所以目前看来还不是.

I want to use classes such as this an entity in Room without having to change its structure since I have many such classes which are auto-generated using code generation tools and there are specific reasons why they need to be backed by a HashMap. Each value of the HashMap should end up as a column in my database table (the key String is an internal implementation detail). Is it possible? Seems not to be at the moment due to how fields are discovered by the annotation processor.

更新:

所有答案都根本不是我想要的.我提出了一个新问题,没有提及HashMap,因为该细节本来就不相关,但所有答案都存在.请参阅如何将Android Room与POJO一起使用是由外部图书馆提供的?

None of the answers were at all what I was looking for. I opened a new question without mentioning HashMap since that detail was not supposed to be relevant but all the answers latched on to it. See How to use Android Room with POJOs provided by an external library? for the updated question.

推荐答案

房间拥有自己的注释-如果您不想映射字段,则必须通过注释POJO来向注释处理器表明这一点;例如. @Entity而不分配tableName以及要忽略的字段上方的@Ignore:

Room has it's own set of annotations - and if you do not want to have a field mapped, you have to indicate this towards the annotation processor by annotating the POJO as it may be required; eg. @Entity without assigning a tableName, as well the @Ignore above the field to ignore:

@Entity
public class City {

    @Ignore
    private final HashMap<String, String> internalMap = new HashMap<>();

    /* concerning the second part of the question: */
    public HashMap<String, Object> toHashMap() {
        return this.internalMap;
    }
}

这些吸气剂还有一个基本问题,即尽管HashMap可能没有用所有(或任何)这些键填充,但它们假定所有键都将存在.并且荒谬的代表具有两种不同类型对象的城市-而其中一种可以完全控制哪些字段正在映射而哪些字段被忽略(一种可能的方法在这里不必排除另一种)...返回HashMap的方法可能很有用,例如.以便将其插入Firebase.

besides those getters have the fundamental problem, that they assume all the keys would exist, despite the HashMap might not have been populated with all (or any) of those keys. and it's absurd to represent a city with two different types of object - while one has full control over which fields are being mapped and which are being ignored (one possible approach does not have to exclude the other here)... a method to return the HashMap might be useful, eg. in order to insert that into Firebase.

我宁愿使用保留值的字段,但仍然能够返回HashMap:

I'd rather go for fields holding the values, while nevertheless being able to return a HashMap:

@Entity(tableName = "cities")
public class City {

    @ColumnInfo(name = "cityId")
    @PrimaryKey(autoGenerate = true)
    private int cityId = -1;

    @ColumnInfo(name = "cityName")
    private String cityName = null;

    ...

    /* concerning the second part of the question: */
    public HashMap<String, Object> toHashMap() {
        HashMap<String, Object> values = new HashMap<>();
        if(this.cityId   !=   -1) {values.put(  "id", this.cityId);}
        if(this.cityName != null) {values.put("name", this.cityName);}
        return values;
    }
}

这样的toHashMap()方法还提供控制,返回的HashMap应该包含哪些字段,而带有TypeConverter&的示例GSON会(按原样)转换整个HashMap.

such a toHashMap() method also provides control, which fields the returned HashMap shall contain, while the example with the TypeConverter & GSON would (as it is) convert the whole HashMap.

这篇关于如何在没有字段的实体上使用Android Room?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆