gpt4 book ai didi

android - 实体中不存在主键中引用的房间

转载 作者:行者123 更新时间:2023-11-29 01:01:50 25 4
gpt4 key购买 nike

我正在尝试在适用于 Android 的 Room Persistance 库中创建多对多关系。我创建了两个实体,MovieEntity 和 GenreEntity。我还创建了一个 MovieJoin 类。基本上,它只是一步步复制本教程:https://android.jlelse.eu/android-architecture-components-room-relationships-bf473510c14a

@Entity(tableName = "genre")
public class GenreEntity {
private String name;
@PrimaryKey
private int id;

public GenreEntity(String name, int id) {
this.name = name;
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return "GenreEntity{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}

    @Entity(tableName = "movie")
public class MovieEntity {
private String originalLanguage;
private String imdbId;
private boolean video;
private String title;
private String backdropPath;
private int revenue;
//private List<GenreEntity> genres;
private double popularity;
@Ignore
private List<ProductionCountriesItem> productionCountries;
@PrimaryKey
private int id;
private int voteCount;
private int budget;
private String overview;
private String originalTitle;
private int runtime;
private String posterPath;
@Ignore
private List<SpokenLanguagesItem> spokenLanguages;
@Ignore
private List<ProductionCompaniesItem> productionCompanies;
private String releaseDate;
private double voteAverage;
@Ignore
private Object belongsToCollection;
private String tagline;
private boolean adult;
private String homepage;
private String status;

public MovieEntity() {
}

public MovieEntity(String originalLanguage, String imdbId, boolean video, String title, String backdropPath, int revenue, double popularity, int id, int voteCount, int budget, String overview, String originalTitle, int runtime, String posterPath, String releaseDate, double voteAverage, Object belongsToCollection, String tagline, boolean adult, String homepage, String status) {
this.originalLanguage = originalLanguage;
this.imdbId = imdbId;
this.video = video;
this.title = title;
this.backdropPath = backdropPath;
this.revenue = revenue;
this.popularity = popularity;
this.id = id;
this.voteCount = voteCount;
this.budget = budget;
this.overview = overview;
this.originalTitle = originalTitle;
this.runtime = runtime;
this.posterPath = posterPath;
this.releaseDate = releaseDate;
this.voteAverage = voteAverage;
this.belongsToCollection = belongsToCollection;
this.tagline = tagline;
this.adult = adult;
this.homepage = homepage;
this.status = status;
}

public String getOriginalLanguage() {
return originalLanguage;
}

public void setOriginalLanguage(String originalLanguage) {
this.originalLanguage = originalLanguage;
}

public String getImdbId() {
return imdbId;
}

public void setImdbId(String imdbId) {
this.imdbId = imdbId;
}

public boolean isVideo() {
return video;
}

public void setVideo(boolean video) {
this.video = video;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getBackdropPath() {
return backdropPath;
}

public void setBackdropPath(String backdropPath) {
this.backdropPath = backdropPath;
}

public int getRevenue() {
return revenue;
}

public void setRevenue(int revenue) {
this.revenue = revenue;
}

public double getPopularity() {
return popularity;
}

public void setPopularity(double popularity) {
this.popularity = popularity;
}

public List<ProductionCountriesItem> getProductionCountries() {
return productionCountries;
}

public void setProductionCountries(List<ProductionCountriesItem> productionCountries) {
this.productionCountries = productionCountries;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getVoteCount() {
return voteCount;
}

public void setVoteCount(int voteCount) {
this.voteCount = voteCount;
}

public int getBudget() {
return budget;
}

public void setBudget(int budget) {
this.budget = budget;
}

public String getOverview() {
return overview;
}

public void setOverview(String overview) {
this.overview = overview;
}

public String getOriginalTitle() {
return originalTitle;
}

public void setOriginalTitle(String originalTitle) {
this.originalTitle = originalTitle;
}

public int getRuntime() {
return runtime;
}

public void setRuntime(int runtime) {
this.runtime = runtime;
}

public String getPosterPath() {
return posterPath;
}

public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}

public List<SpokenLanguagesItem> getSpokenLanguages() {
return spokenLanguages;
}

public void setSpokenLanguages(List<SpokenLanguagesItem> spokenLanguages) {
this.spokenLanguages = spokenLanguages;
}

public List<ProductionCompaniesItem> getProductionCompanies() {
return productionCompanies;
}

public void setProductionCompanies(List<ProductionCompaniesItem> productionCompanies) {
this.productionCompanies = productionCompanies;
}

public String getReleaseDate() {
return releaseDate;
}

public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}

public double getVoteAverage() {
return voteAverage;
}

public void setVoteAverage(double voteAverage) {
this.voteAverage = voteAverage;
}

public Object getBelongsToCollection() {
return belongsToCollection;
}

public void setBelongsToCollection(Object belongsToCollection) {
this.belongsToCollection = belongsToCollection;
}

public String getTagline() {
return tagline;
}

public void setTagline(String tagline) {
this.tagline = tagline;
}

public boolean isAdult() {
return adult;
}

public void setAdult(boolean adult) {
this.adult = adult;
}

public String getHomepage() {
return homepage;
}

public void setHomepage(String homepage) {
this.homepage = homepage;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

@Entity(tableName = "movie_join",
primaryKeys = { "movieId, genreId"},
foreignKeys = {
@ForeignKey(entity = MovieEntity.class,
parentColumns = "id",
childColumns = "movieId"),
@ForeignKey(entity = GenreEntity.class,
parentColumns = "id",
childColumns = "genreId")
}
)
public class MovieJoin {
private int movieId;
private int genreId;

public MovieJoin(int movieId, int genreId) {
this.movieId = movieId;
this.genreId = genreId;
}

public int getMovieId() {
return movieId;
}

public void setMovieId(int movieId) {
this.movieId = movieId;
}

public int getGenreId() {
return genreId;
}

public void setGenreId(int genreId) {
this.genreId = genreId;
}

我收到以下错误:错误:实体中不存在主键中引用的 movieId、genreId。可用列名:movieId、genreId。求助,怎么了?

最佳答案

所以,问题是我忘记为我的类声明从 RoomDatabase 扩展的 GenreEntity。

@Database(entities = {
MovieListItemEntity.class,
MovieEntity.class,
GenreEntity.class
}

出于某种原因,Android Studio 并没有对此提出提示。所以现在它开始工作了。

关于android - 实体中不存在主键中引用的房间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543214/

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