gpt4 book ai didi

Java android ROOM获取具有详细信息的对象

转载 作者:行者123 更新时间:2023-11-30 05:12:38 25 4
gpt4 key购买 nike

我有这个:

    @Entity(tableName = "director"/*,
indices = {@Index(value = "full_name", unique = true)}*/)
public class Director {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "did")
public int id;
@ColumnInfo(name = "full_name")
@NonNull
public String fullName;


@Entity(tableName = "movie",
foreignKeys = @ForeignKey(entity = Director.class,
parentColumns = "did",
childColumns = "directorId",
onDelete = ForeignKey.CASCADE),
indices = {@Index("title"), @Index("directorId")})
public class Movie {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "mid")
public int id;
@ColumnInfo(name = "title")
@NonNull
public String title;
@ColumnInfo(name = "directorId")
public int directorId;

Amd 当我获取所有电影时,我会获取导演 ID。我应该怎么做才能生成这个:

public class MovieWithDirector {

Movie movie;
Director director;
}

我可以获得一个用户,然后再获得 directerByID,但我现在不知道这是不是最好的方法

最佳答案

我会说执行此操作的更好方法是使用 join 编写查询:

SELECT  movie.title, director.fullname 
FROM director
INNER JOIN movie
ON movie.directorId=director.id
WHERE director.id= :passed_value

当然,您需要自定义类来存储查询结果。如果您检查此 link 中的示例,并结合上面的查询,然后你就可以解决你的问题。

关于Java android ROOM获取具有详细信息的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53476030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com