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
}
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.
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?
JSON.parse() The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
Use type converters You support custom types by providing type converters, which are methods that tell Room how to convert custom types to and from known types that Room can persist. You identify type converters by using the @TypeConverter annotation.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With