我有两个 Room 实体:
@Entity(tableName = LocationsTable.NAME)
data class LocationDto(@PrimaryKey val timestamp: Long,
val latitude: Double,
val longitude: Double,
val altitude: Double)
@Entity(tableName = TripsTable.NAME)
data class TripDto(@PrimaryKey(autoGenerate = true) val _id: Long,
val startTime: Long,
val endTime: Long)
现在我正在尝试执行 JOIN 以获得这样的对象:
data class TripWithLocationDto(val startTime: Long,
val endTime: Long,
val locations: List<LocationDto>)
位置必须包含行程实体的开始时间和结束时间之间的 locationDto。查询应类似于:
SELECT trips_table.start_time, trips_table.end_time FROM trips_table JOIN locations_table WHERE locations_table.timestamp >= trips_table.start_time AND locations_table.timestamp <= trips_table.end_time
所有文章仅描述了使用外键的实现。 谁知道如何让它工作?
最佳答案
您在要连接的实体类中创建外键,然后创建另一个实体类,在其中获取要连接的两个表的字段,并像上面在 dao 中所做的那样放置连接查询。您也可以查看以下链接。在多对多关系标签中
https://android.jlelse.eu/android-architecture-components-room-relationships-bf473510c14a
您还可以访问以下 kotlin 中的链接:-
http://danielgaribaldi.com/room-persistence-library-part-2-room-relationships/
关于android - 使用 Room 加入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51265904/