我已经看到此问题room-error-the-columns-returned-by-the-query-does-not-have-the-fields-fieldname,但无法将其应用于自己的错误。
这是我的Movie
班
@Entity
public class Movie {
@PrimaryKey
@NonNull
public String id;
@ColumnInfo
public String name;
@ColumnInfo
public String title;
}
这是我的UpcomingMovies
班
@Entity(
foreignKeys = {
@ForeignKey(
entity = Movie.class,
parentColumns = "id",
childColumns = "movieId")
},
indices = @Index("movieId")
)
@TypeConverters(MovieConverter.class)
public class UpcomingMovies {
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo
public int movieId;
@ColumnInfo
public Movie movie;
}
我要运行的查询,我想获取电影列表
@Query("Select movie from UpcomingMovies")
LiveData<List<Movie>> loadUpcomingMovies();
还有我遇到的具体错误
错误:查询返回的列在* .Model.POJOs.Movie中没有字段[id],即使它们被注释为非null或原始类型。查询返回的列:[电影]
我在这里错过了什么?
编辑:添加了MovieConverter
public class MovieConverter {
@TypeConverter
public String movieToString(Movie movie) {
return new Gson().toJson(movie);
}
@TypeConverter
public Movie stringToMovie(String src) {
return new Gson().fromJson(src, Movie.class);
}
}
答案 0 :(得分:1)
尝试此查询:
@Query("Select * from UpcomingMovies")
LiveData<List<UpcomingMovies>> loadUpcomingMovies();
在您的UpcomingMovies
类中具有GETTER方法:
public Movie getMovie() {
return this.movie;
}
您还可以通过此查询获取电影列表:
@Query("Select * from Movie")
LiveData<List<Movie>> loadUpcomingMovies();
祝你好运:)