I try to refactor some code and move several architecture to use Room Database from architecture components.
I have such object, which i use often, getting it from cache. Here what it looks like:
public class LocationEvents {
private Map<NexoIdentifier, Date> mDeviceFirstSeenDates;
private ArrayDeque<LocationGeoEvent> mGeoEvents;
private ArrayDeque<LocationRSSIEvent> mRSSIEvents;
private Map<NexoIdentifier, ScoredLocationEvent> mHighestScores;
///Some methods
}
I want to model database with this structure. So there will be entities like LocationGeoEvent, LocationRSSIEvent, ScoredLocationEvent.
They look like this:
public class LocationGeoEvent {
private double mLongitude;
private double mLatitude;
private double mAccuracy;
private Date mTimestamp;
}
public class LocationRSSIEvent {
private int mRSSI;
private NexoIdentifier mNexoIdentifier;
private Date mTimestamp;
}
public class ScoredLocationEvent {
private float mScore;
private NexoIdentifier mNexoIdentifier;
private LocationRSSIEvent mLocationRSSIEvent;
private LocationGeoEvent mLocationGeoEvent;
private Date mScoreCalculatedTime;
private boolean mSent;
private boolean mPreviousSent;
}
NexoIdentifier is a simple POJO:
class NexoIdentifier {
abstract val partialSerialID: String
abstract val id: String
abstract val countryAndManufacturer: String
}
So how can i make the relations using Room? Is it even possible to make the LocationEvent entity as once? So for example i want to be able to fetch LocationEvent with all of this list that are nested inside. Or maybe there is another way to do this? Also don't know exactly how to model those 2 maps inside LocationEvents - DeviceFirstSeenDates, and HighestScores - as two separate Entities with relation to others? But how exactly? I will really appreciate help in this example, I am really stuck
UPDATE
@Entity(tableName = "location_events")
data class LocationEvents(
@PrimaryKey(autoGenerate = true)
val id: Long = 0,
@Embedded(prefix = "device") val mDeviceFirstSeenDates: Map<NexoIdentifier, Date> = HashMap(),
@Embedded(prefix = "events") val mGeoEvents: ArrayDeque<LocationGeoEvent> = ArrayDeque(),
val mRSSIEvents: ArrayDeque<LocationRSSIEvent> = ArrayDeque(),
val mHighestScores: Map<NexoIdentifier, ScoredLocationEvent> = HashMap()
) {
constructor() : this(0L, hashMapOf<NexoIdentifier, Date>(),
ArrayDeque(), ArrayDeque(), hashMapOf<NexoIdentifier, ScoredLocationEvent>()
)
}
Error:error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).