Reputation: 658
I have the following Model class that i want to store in Room Database:
I don't know much but i think i need to use foreign key here. Though i dont have much idea, its just a guess.
If you can give a detailed explanation,
public class EarthquakeData {
public List < Feature > features;
public class Feature {
public String type;
public Properties properties;
// public Geometry geometry;
public String id;
public class Properties {
public double mag;
public String place;
public Object time;
public Object updated;
public Object tz;
public String url;
public String detail;
public int felt;
public double cdi;
public double mmi;
public String alert;
public String status;
public int tsunami;
public int sig;
public String net;
public String code;
public String ids;
public String sources;
public String types;
public int nst;
public double dmin;
public double rms;
public double gap;
public String magType;
public String type;
public String title;
}
}
}
I have saved only a simple class in Room database like the following class:
@Entity(tableName = "notes")
public class Note {
@PrimaryKey
@NonNull
private String id;
@NonNull
@ColumnInfo(name = "note")
private String mNote;
public Note(@NonNull String id, @NonNull String mNote) {
this.id = id;
this.mNote = mNote;
}
@NonNull
public String getId() {
return id;
}
@NonNull
public String getNote() {
return this.mNote;
}
}
But i don't know to save the first type of model class in Room which is very complex for me because it consists of objects within a class.
Upvotes: 0
Views: 87
Reputation: 7700
From what I understand you have the following relationships:
1 EarthquakeData - Many Features
and
1 Feature - 1 Properties
So you could model it this way:
Table Features(id: PrimaryKey, earthquake_data_id: ForeignKey, ...)
and
Table Feature(id: PrimaryKey, properties_id: ForeignKey, ...)
With Room entities
this would look something like this for the features:
@Entity(
tableName = "features",
foreignKeys = [
ForeignKey(
entity = EarthquakeDataEntity::class,
parentColumns = ["id"],
childColumns = ["earthquake_data_id"],
onDelete = ForeignKey.CASCADE, // See documentation for other operations
onUpdate = ForeignKey.CASCADE
)
]
class Features {
...
}
Check out the ForeignKey official documentation for the parameters
Upvotes: 1