You can do this using kind of two ways. See what fits better to your situation.
TypeConverter: With type converters defined, you can use your custom type in your entities and DAOs just as you would use primitive types
Example:
public static class Converters {
@TypeConverter
public Date fromTimestamp(Long value) {
return value == null ? null : new Date(value);
}
@TypeConverter
public Long dateToTimestamp(Date date) {
if (date == null) {
return null;
} else {
return date.getTime();
}
}
@TypeConverter
public Project fromString(String value) {
return new Gson().fromJson(value, Project.class);
}
@TypeConverter
public String fromProject(Project project) {
return new Gson().toJson(project);
}
}
Then you can add @TypeConverters(Converters.class)
annotation on your abstract RoomDatabase class.
Embedded: This annotation indicates that the instance of the class being embedded is stored as an intrinsic part of the class where the annotation is being used.
Example:
public class Project {
double projectLatitude;
double ProjectLongitude;
String projectName;
}
@Entity(tableName = "tbl_projects")
public class Room_Database_Project {
@PrimaryKey(autoGenerate = true)
private int id;
@Embedded
private Project project;
}
https://developer.android.com/training/data-storage/room/referencing-data