Android Room:将json结果转换为db对象的有效方法 [英] Android Room: Efficient way to transform json result into db object
问题描述
我有一个看起来像这样的API调用解析的POJO
I have a POJO parsed from an API call which looks like this
public class Article {
public Long id;
@Expose
@SerializedName("section")
public String section;
@Expose
@SerializedName("title")
public String title;
@Expose
@SerializedName("topics")
public List<String> topics;
@Expose
@SerializedName("media")
public List<Media> media;
}
为了最大限度地减少冗余和重复,我正在寻找创建这样的模式
For the sake of minimizing redundancy and duplicates, I am looking to create a schema like this
@Entity(foreignKeys = {
@ForeignKey(entity = Article.class, parentColumns = "id", childColumns = "articleId"),
@ForeignKey(entity = Topic.class, parentColumns = "id", childColumns = "topicId"),
@ForeignKey(entity = Media.class, parentColumns = "id", childColumns = "mediaId")
}
public class Articles {
@PrimaryKey
public Long articleId;
@ColumnInfo(name = "topic_id")
public Long topicId;
@ColumnInfo(name = "media_id")
public Long mediaId;
}
@Entity
public class Article {
// Left out
}
@Entity
public class Media {
// Left out
}
如您所见,当我调用DAO方法访问数据库时,我不能只是直接传递pojo对象(除非我对此有误).我相信我需要将对象转换为与数据库实体模型匹配的对象.
As you can see, when I call the DAO methods to access the database, I can not just pass in the pojo object directly (unless I am mistaken about this). I believe I need to transform the object to one which matches the database entity model.
Android框架是否提供从POJO转换为数据库模型对象的自然方法?除了自己手动转换外,还有其他方法吗?
Does the Android Framework provide a natural way to convert from POJO to the database model object? Is there a way to do this other than manually converting it myself?
- 我知道我可以在DAO接口内的方法内实现转换.但是然后,我将不得不创建一个新对象并手动设置所有值.
- 最初,我认为类型转换器可以工作,但它们似乎可以转换单个列.
推荐答案
您要做的就是为POJO(模型类)使用 @Embedded
批注参考另一堂课.然后创建一个类型转换器类.
All you have to do is use @Embedded
annotation for your POJO(Model Class) which will refer to another class. then create a type converter class.
@Embedded(prefix = "media")
private Meida media;
@TypeConverters({TypeConvertorClass.class})
@Database(entities = {Article .class,Media.class}, version = 1, exportSchema = false)
public abstract class `DataBaseExample` extends RoomDatabase {
}
public class Converters {
@TypeConverter
public static ArrayList<String> fromString(String value) {
Type listType = new TypeToken<ArrayList<String>>() {}.getType();
return new Gson().fromJson(value, listType);
}
@TypeConverter
public static String fromArrayLisr(ArrayList<String> list) {
Gson gson = new Gson();
String json = gson.toJson(list);
return json;
}
}
public class TypeConvertorClass {
@TypeConverter
public static Media getMedia(String longId) {
return longId== null ? null : new Meida();
}
}
@Entity(tableName = "Article")
public class Article {
@ColumnInfo (name = "article_id")
public Long id;
@Expose
@SerializedName("section")
public String section;
@Expose
@SerializedName("title")
public String title;
@Expose
@SerializedName("topics")
public List<String> topics;
@Embedded(prefix = "media") // We need relation to Media table
@Expose
@SerializedName("media")
public List<Media> media;
}
public class Media {
@ColumnInfo (name = "media_id")
public Long id;
}
这篇关于Android Room:将json结果转换为db对象的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!