I’m working with Room database in Android and have three data classes representing my tables:
data class Train(id: Int, isFollow: Boolean)
data class TrainStatus(id: Int, trainId: Int, origin: String, destination: String)
data class PreviousTrain(id: Int, trainStatusId: Int, previousTrainStatusId: Int)
In TrainStatus, trainId is a foreign key that references id from the Train class.
PreviousTrain is a junction table where trainStatusId and previousTrainStatusId are foreign keys referencing id from TrainStatus.
I want to query the database and get a response in the following format:
data class Response(
val train: Train,
val trainStatusWithPreviousTrain: List<TrainStatusWithPreviousTrain>
)
data class TrainStatusWithPreviousTrain(
val trainStatus: TrainStatus,
val previousTrain: TrainStatus
)
How can I use the junction table to create a Room query that will give me the data in the Response format?