错误:无法找出如何将该字段保存到模型类中的数据库中

时间:2019-05-19 10:34:14

标签: java android-room

我正在开发一个新的应用程序,但是我的代码出现以下错误:

  

无法弄清楚如何将该字段保存到数据库中。您可以   考虑为其添加类型转换器。

我已经尝试了stackoverflow的所有可能的解决方案,但是没有用。

在我的Article.java模型类下面

@Entity(
        tableName = "article",
        foreignKeys = @ForeignKey(
                entity = Source.class,
                parentColumns = "id",
                childColumns = "source"
        ))

public class Article {
    @PrimaryKey
    @SerializedName("source")
    @NonNull
    @Expose

    @ColumnInfo(name ="source")
    private Source source;

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    @SerializedName("author")
    @Expose
    @ColumnInfo(name = "author")
    private String author;
    @SerializedName("title")
    @Expose
    @ColumnInfo(name = "title")
    private String title;
    @SerializedName("description")
    @Expose
    @ColumnInfo(name = "description")
    private String description;
    @SerializedName("url")
    @Expose
    @ColumnInfo(name = "url")
    private String url;
    @SerializedName("urlToImage")
    @Expose
    @ColumnInfo(name = "urlToImage")
    private String urlToImage;

    @SerializedName("publishedAt")
    @Expose
    @ColumnInfo(name = "publishedAt")
    private String publishedAt;
    @SerializedName("content")
    @Expose
    @ColumnInfo(name = "content")
    private String content;





    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlToImage() {
        return urlToImage;
    }

    public void setUrlToImage(String urlToImage) {
        this.urlToImage = urlToImage;
    }

    public String getPublishedAt() {
        return publishedAt;
    }

    public void setPublishedAt(String publishedAt) {
        this.publishedAt = publishedAt;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

低于Source.java

道课以下

@Dao
public interface SportNewsDao {

    @Query("SELECT * FROM article")
    List<Article> getArticles();

    @Insert
    void insertAll(Article... article);
    @Delete
    void delete(Article article);

    @Update
    void update(Article article);

}

在数据库类下面

@Database(entities = {Article.class,Source.class}, version = 1, exportSchema = false)
public  abstract class SportNewsDatabase extends RoomDatabase {
  public  abstract SportNewsDao sportNewsDao();
}

below SourceTypeConverter.java
public class  SourceTypeConverter {

    @TypeConverter
    public static Source ConvertSource(Source source){
        return source ==   null ? null : new Source(source);
    }

}

2 个答案:

答案 0 :(得分:1)

要使用您的SourceTypeCoverter,您需要在SportNewsDatabase类中添加以下注释

@TypeConverters(SourceTypeConverter.class)

此外,您的SourceTypeConverter可能缺少方法。您需要一种方法将Object转换为某种形式的图元,然后从该图元转换回Object。像这样

public class  SourceTypeConverter {
    @TypeConverter
    public static String ConvertSource(Source source){
        return source == null ? null : source.toString();
    }

    @TypeConverter
    public static Source ConvertSource(String source){
        return source == null ? null : new Source(source);
    }
}

您可以在这里Referencing Complex data using room

了解更多信息

请记住,使用toString()可能对此无效。您可能需要使用Gson之类的东西将您的对象转换为字符串并返回。


如果这不起作用,您可以随时尝试@embeddedAndroid Room Embedded

答案 1 :(得分:0)

要让Room查找SourceTypeConverter,您必须使用以下注释注释数据库,Dao或实体来注册它:

@TypeConverters(SourceTypeConverter.class)