我正在尝试在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;
}
}
我收到以下错误:error: movieId, genreId referenced in the primary key does not exists in the Entity. Available column names:movieId, genreId
。
请帮忙,出了什么问题?
答案 0 :(得分:1)
所以,问题是我忘了为从RoomDatabase扩展的类声明GenreEntity。
@Database(entities = {
MovieListItemEntity.class,
MovieEntity.class,
GenreEntity.class
}
由于某些原因,Android Studio没有抱怨这一点。所以现在它正在工作。