房间数据库查询几个实体

发布于 2025-01-30 19:38:32 字数 1602 浏览 7 评论 0原文

请你帮助我好吗?我尝试通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2025-02-06 19:38:32

第一个问题是如何制作@query请求(在@dao接口中),这将帮助我在RecyClerview中显示流行的电影名称,标题,类型 +电影持续时间。我想他们应该通过电影ID映射它们...

因为电影与持续时间之间存在一对一的关系,然后您可以通过在电影中包括持续时间字段来简化问题< /strong>。例如,而不是移动响应和移动化,而只是移动响应为: -

@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>,
    @ColumnInfo(name = "duration")
    val runtime: Int
)

否则,您通常会有一个POJO类,反映了两个具有两个字段的clasess/entities/entities/table;一个是移动响应,另一个是移动。

例如

选项1 (通过@embedded注释): -

data class MovieWithEmbeddedDuration(
    @Embedded
    val movieResponse: MovieResponse,
    @Embedded(prefix = "duration_") /* prefix required to disambiguate movie_id column that is in both */
    val movieDuration: MovieDuration
)
  • 这将需要使用加入,例如@query(“ select Movie.*,Movie_duration.movi​​e_id作为duration_movie_id,movie_duration.duration as duration_duration from Movie con Movie_duration on Movie.movi​​e_id = movie_duration.movi​​e_id')Fun getMoviesWitheMbedDedDuration()
    • 请注意,由于需要消除显示在两个表中的Movie_ID列而引起的复杂性。 AS 用于在输出时更改列的名称(具有唯一的列名称,因为Movie_id是参考/映射/与父的关系,也许可以将列命名为“ Movie_id_map”列描述列的用法)。

选项2 (通过@RELATION注释)

data class MovieWithRelatedDuration(
    @Embedded
    val movieResponse: MovieResponse,
    @Relation(
        entity = MovieDuration::class,
        parentColumn = "movie_id",
        entityColumn = "movie_id"
    )
    val movieDuration: MovieDuration
)
  • 这不需要加入您简单地使用查询来获取父(s),但获取带有@Relation eg eg @transaction @query(“ select * from Movie”)的pojo fun getMoviesWithRelelatedDuration():list&lt; moviewithRelelatedDuration&gt;
  • in @relation @relation @relation @relation @relation work ut 它有效通过运行单独的查询以获取孩子(模仿加入),因此为什么 @transaction 注释(不需要,但如果省略了)。因此,这效率较低。

选项2 (通过持续时间为字段)

而不是嵌入移动对象,您可以仅提取持续时间并具有该字段,例如

data class MovieWithDurationAsField(
    @Embedded
    val movieResponse: MovieResponse,
    val duration: Int
)
  • ,可以使用@query( “选择电影。

  • 。被使用因此,这是有效的,而不是根据与其名称匹配的列确定字段的值。因此,如果使用运行时,则必须重命名从持续时间到运行时的输出列。

第二个问题是如何提出@query请求,这将帮助我将我点击的确切电影与电影演员联系在一起?

基本上已经在上面解释了这一点(请参阅选项2)。用父@embedded和带有@RELATION注释的孩子创建POJO,而val是子对象的列表/数组,而不是一个对象。

例如: -

data class MovieWithCastList(
    @Embedded
    val movieResponse: MovieResponse,
    @Relation(
        entity = Cast::class,
        parentColumn = "movie_id",
        entityColumn = "movie_id"
    )
    val castList: List<Cast>
)
  • 查询/函数将符合@query(“ select * from Movie”)fun getMoviewIthCastList():list&lt; moviewithcastlist&gt;

  • note 显然带有Retrofit您相应地调整了查询​​。

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...

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:-

@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>,
    @ColumnInfo(name = "duration")
    val runtime: Int
)

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

data class MovieWithEmbeddedDuration(
    @Embedded
    val movieResponse: MovieResponse,
    @Embedded(prefix = "duration_") /* prefix required to disambiguate movie_id column that is in both */
    val movieDuration: MovieDuration
)
  • This would require the use of a JOIN such as @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>
    • note the complexity due to the need to disambiguate the movie_id column which appears in both tables. AS is used to alter the name of the column when it is output (much easier to have unique column names, as the movie_id is a reference/map/relation to the parent perhaps name the column movie_id_map which better describes the column's usage).

Option 2 (via @Relation annotation)

data class MovieWithRelatedDuration(
    @Embedded
    val movieResponse: MovieResponse,
    @Relation(
        entity = MovieDuration::class,
        parentColumn = "movie_id",
        entityColumn = "movie_id"
    )
    val movieDuration: MovieDuration
)
  • this doesn't require the join you simple use a query to get the parent(s) BUT get the POJO with @relation e.g. @Transaction @Query("SELECT * FROM movie") fun getMoviesWithRelatedDuration(): List<MovieWithRelatedDuration>
  • Here @Relation does the work BUT it works by running separate queries to get the children (mimicking the JOIN), hence why the @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.

data class MovieWithDurationAsField(
    @Embedded
    val movieResponse: MovieResponse,
    val duration: Int
)
  • 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.

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?

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:-

data class MovieWithCastList(
    @Embedded
    val movieResponse: MovieResponse,
    @Relation(
        entity = Cast::class,
        parentColumn = "movie_id",
        entityColumn = "movie_id"
    )
    val castList: List<Cast>
)
  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文