简体   繁体   English

使用Room加入查询

[英]Joining a query by using Room

I have two Room entities: 我有两个房间实体:

@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)

And now I'm trying to do a JOIN to get an object like this: 现在我正在尝试JOIN来获取这样的对象:

data class TripWithLocationDto(val startTime: Long,
                        val endTime: Long,
                        val locations: List<LocationDto>)

Locations must contain locationDtos between start and end times of a trip entity. 位置必须包含trip实体的开始和结束时间之间的locationDtos。 The query should look similar to: 查询应类似于:

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

All articles describe the implementation with foreign keys only. 所有文章仅描述使用外键的实现。 Who knows how to make it work? 谁知道如何使它工作?

You create foreign keys in the entity class which you want to join, and after that make another entity class in which you take field of both tables you are joining and put join query as you made above in dao. 您在要加入的实体类中创建外键,然后创建另一个实体类,在该实体类中,您将加入两个表的字段,并按照上面的dao进行连接查询。 You can check below link also. 您也可以查看以下链接。 in many to many relationship tag 在许多对很多关系标签

https://android.jlelse.eu/android-architecture-components-room-relationships-bf473510c14a https://android.jlelse.eu/android-architecture-components-room-relationships-bf473510c14a

You can also visit below link which is in kotlin:- 您还可以访问位于kotlin的以下链接: -

http://danielgaribaldi.com/room-persistence-library-part-2-room-relationships/ http://danielgaribaldi.com/room-persistence-library-part-2-room-relationships/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM