房间数据库查询几个实体
请你帮助我好吗?我尝试通过Retrofit2从TMDB获取数据库,并在RecyClerview中显示。我拨打3个API电话,以获取有关流行电影ID/海报/概述/标题/流派的数据,第二个电话是我按电影ID获得电影持续时间,并且使用第三个电话,我将通过电影ID播放电影。
@GET("movie/popular")
suspend fun getPopularMovies(@Query("api_key") apiKey: String?): PopularResponseModel
@GET("/movie/{movie_id}")
suspend fun getMovieDuration(@Query("api_key") apiKey: String?): MovieDuration
@GET("/movie/{movie_id}/credits")
suspend fun getCrewAndCast(@Query("api_key") apiKey: String?): Cast
我得到了3种型号:
@Entity(tableName = "movie")
data class MovieResponse(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "movie_id")
val id: Int,
@ColumnInfo(name = "movie_image")
val poster_path: String,
@ColumnInfo(name = "movie_overview")
val overview: String,
@ColumnInfo(name = "movie_title")
val title: String,
@ColumnInfo(name = "movie_genres")
val genre_ids: ArrayList<Int>)
@Entity(tableName = "movie_duration")
data class MovieDuration(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "movie_id")
val id: Int,
@ColumnInfo(name = "duration")
val runtime: Int
)
@Entity(tableName = "cast")
data class Cast(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "movie_id")
val id : Int,
val cast :ArrayList<Cast>
)
第一个问题是如何制作@Query请求(在@dao接口中),这将帮助我在RecyClerview中显示流行的电影名称,标题,类型 +电影持续时间。我想他们应该通过电影ID来映射它们...
例如(John Wick-行动/犯罪,2h 10m)。
第二个问题是如何提出@query请求,这将帮助我将我点击的确切电影与电影演员联系起来?
(电影约翰·威克 - 约翰·威克:基努·里夫斯...)
Could you please help me? I try to get data from TMDB via Retrofit2 to Room database and display it in Recyclerview. I make 3 api calls to get data about popular movie id/poster/overview/title/genres, with the second call I get movie duration by movie id, and with the 3rd call I get movie cast by movie id.
@GET("movie/popular")
suspend fun getPopularMovies(@Query("api_key") apiKey: String?): PopularResponseModel
@GET("/movie/{movie_id}")
suspend fun getMovieDuration(@Query("api_key") apiKey: String?): MovieDuration
@GET("/movie/{movie_id}/credits")
suspend fun getCrewAndCast(@Query("api_key") apiKey: String?): Cast
And I get 3 models:
@Entity(tableName = "movie")
data class MovieResponse(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "movie_id")
val id: Int,
@ColumnInfo(name = "movie_image")
val poster_path: String,
@ColumnInfo(name = "movie_overview")
val overview: String,
@ColumnInfo(name = "movie_title")
val title: String,
@ColumnInfo(name = "movie_genres")
val genre_ids: ArrayList<Int>)
@Entity(tableName = "movie_duration")
data class MovieDuration(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "movie_id")
val id: Int,
@ColumnInfo(name = "duration")
val runtime: Int
)
@Entity(tableName = "cast")
data class Cast(
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "movie_id")
val id : Int,
val cast :ArrayList<Cast>
)
The first question is how do I make a @Query request (in the @Dao interface) that will help me to display the popular movie name, title, genre + movie duration associated with that movie in a Recyclerview. I guess they should be mapped somehow by movie id...
For example (John Wick - Action/crime, 2h 10m).
The second question is how to make a @Query request that will help me to associate the exact movie I click on with the movie cast?
(movie John Wick - John Wick: Keanu Reeves...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为电影与持续时间之间存在一对一的关系,然后您可以通过在电影中包括持续时间字段来简化问题< /strong>。例如,而不是移动响应和移动化,而只是移动响应为: -
否则,您通常会有一个POJO类,反映了两个具有两个字段的clasess/entities/entities/table;一个是移动响应,另一个是移动。
例如
选项1 (通过
@embedded
注释): -@query(“ select Movie.*,Movie_duration.movie_id作为duration_movie_id,movie_duration.duration as duration_duration from Movie con Movie_duration on Movie.movie_id = movie_duration.movie_id')Fun getMoviesWitheMbedDedDuration()- 请注意,由于需要消除显示在两个表中的Movie_ID列而引起的复杂性。 AS 用于在输出时更改列的名称(具有唯一的列名称,因为Movie_id是参考/映射/与父的关系,也许可以将列命名为“ Movie_id_map”列描述列的用法)。
选项2 (通过
@RELATION
注释)@transaction @query(“ select * from Movie”)的pojo fun getMoviesWithRelelatedDuration():list&lt; moviewithRelelatedDuration&gt;
@transaction
注释(不需要,但如果省略了)。因此,这效率较低。选项2 (通过持续时间为字段)
而不是嵌入移动对象,您可以仅提取持续时间并具有该字段,例如
,可以使用
@query( “选择电影。
基本上已经在上面解释了这一点(请参阅选项2)。用父
@embedded
和带有@RELATION
注释的孩子创建POJO,而val是子对象的列表/数组,而不是一个对象。例如: -
查询/函数将符合
@query(“ select * from Movie”)fun getMoviewIthCastList():list&lt; moviewithcastlist&gt;
As there is a one to one relationship between movie and it's duration then you could simplify matters by including the duration field in the movie. For example instead of MovieResponse and MovieDuration have just MovieResponse as:-
Otherwise you would typically have a POJO class that reflects the two clasess/entities/tables that has two fields; one a MovieResponse and the other a MovieDuration.
e.g.
Option 1 (via
@Embedded
annotation):-@Query("SELECT movie.*, movie_duration.movie_id AS duration_movie_id, movie_duration.duration AS duration_duration FROM movie JOIN movie_duration ON movie.movie_id = movie_duration.movie_id") fun getMoviesWithEmbeddedDuration(): List<MovieWithEmbeddedDuration>
Option 2 (via
@Relation
annotation)@Transaction @Query("SELECT * FROM movie") fun getMoviesWithRelatedDuration(): List<MovieWithRelatedDuration>
@Transaction
annotation (not needed but warned if omitted). Thus this is less efficient.Option 2 (via field for the duration)
Instead of embedding the MovieDuration object, you could extract just the duration and have a field for that e.g.
This could be used using
@Query("SELECT movie.*,duration FROM movie JOIN movie_duration ON movie_duration.movie_id = movie.movie_id") fun getMoviesWithDurationAsField(): List<MovieWithDurationAsField>
A JOIN is used so this is efficient not that the value for the field is determined according to the column that matches it's name. So if runtime were used then you'd have to rename the output column from duration to runtime.
This has basically been explained above (see Option 2). Create the POJO with the parent
@Embedded
and the children with@Relation
annotation and the val being a List/Array of the child objects rather than a single object.e.g. something like:-
The Query/function would be along the lines of
@Query("SELECT * FROM movie") fun getMovieWithCastList(): List<MovieWithCastList>
Note obviously with Retrofit you adapt the queries accordingly.