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

时间:2018-12-17 09:49:45

标签: android android-room android-architecture-components

我有波纹管模型:

@Entity(foreignKeys = @ForeignKey(
        entity = ClassModel.class,
        childColumns = "classModelId",
        parentColumns = "id",
        onDelete = ForeignKey.CASCADE)
)
public class _0 {

    @PrimaryKey(autoGenerate = true)
    private long id;

    private long classModelId;

    @SerializedName("userinfo")
    @Expose
    private Userinfo userinfo;

    @SerializedName("enrolls")
    @Expose
    private List<Enroll> enrolls = null;


    // --- CONSTRUCTORS ---
    @Ignore
    public _0(){

    }
    public _0(long id, long classModelId, Userinfo userinfo, List<Enroll> enrolls) {
        this.id = id;
        this.classModelId = classModelId;
        this.userinfo = userinfo;
        this.enrolls = enrolls;
    }

    // --- GETTER ---
    public long getId() {
        return id;
    }

    public long getClassModelId() {
        return classModelId;
    }

    public List<Enroll> getEnrolls() {
        return enrolls;
    }

    public Userinfo getUserinfo() {
        return userinfo;
    }

    // --- SETTER ---
    public void setEnrolls(List<Enroll> enrolls) {
        this.enrolls = enrolls;
    }

    public void setUserinfo(Userinfo userinfo) {
        this.userinfo = userinfo;
    }

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

    public void setClassModelId(long classModelId) {
        this.classModelId = classModelId;
    }
}

让我看到这个错误error: Cannot figure out how to save this field into database. You can consider adding a type converter for it下面的行:

@SerializedName("userinfo")
@Expose
private Userinfo userinfo;

@SerializedName("enrolls")
@Expose
private List<Enroll> enrolls = null;

Userinfo就像下面这样:

@Entity(foreignKeys = @ForeignKey(
        entity = _0.class,
        childColumns = "_0Id",
        parentColumns = "id",
        onDelete = ForeignKey.CASCADE)
)
public class Userinfo {

    @PrimaryKey(autoGenerate = true)
    private long id;

    private long _0Id;

    @SerializedName("userid")
    @Expose
    private String userid;
    @SerializedName("eduid")
    @Expose
    private String eduid;
    @SerializedName("cnt")
    @Expose
    private Integer cnt;


    // --- CONSTRUCTORS ---
    public Userinfo(){

    }
    public Userinfo(long id, long _0Id, String userid, String eduid, Integer cnt) {
        this.id = id;
        this._0Id = _0Id;
        this.userid = userid;
        this.eduid = eduid;
        this.cnt = cnt;
    }

    // --- GETTER ---
    public String getUserid() {
        return userid;
    }

    public long getId() {
        return id;
    }

    public long get_0Id() {
        return _0Id;
    }

    public String getEduid() {
        return eduid;
    }

    public Integer getCnt() {
        return cnt;
    }

    // --- SETTER ---
    public void setId(long id) {
        this.id = id;
    }

    public void set_0Id(long _0Id) {
        this._0Id = _0Id;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public void setEduid(String eduid) {
        this.eduid = eduid;
    }

    public void setCnt(Integer cnt) {
        this.cnt = cnt;
    }
}

List<Enroll>是:

@Entity(foreignKeys = @ForeignKey(
        entity = _0.class,
        childColumns = "_0Id",
        parentColumns = "id",
        onDelete = ForeignKey.CASCADE)
)
public class Enroll {

    private long _0Id;
    @SerializedName("username")
    @Expose
    private String username;
    @PrimaryKey
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("course_id")
    @Expose
    private String courseId;
    @SerializedName("course_name")
    @Expose
    private String courseName;
    @SerializedName("category_id")
    @Expose
    private String categoryId;
    @SerializedName("category_name")
    @Expose
    private String categoryName;
    @SerializedName("teacher")
    @Expose
    private List<Teacher> teacher = null;
    @SerializedName("is_teacher")
    @Expose
    private Integer isTeacher;
    @SerializedName("course_info")
    @Expose
    private CourseInfo courseInfo;
    @SerializedName("assign")
    @Expose
    private Assign assign;
    @SerializedName("quiz")
    @Expose
    private Quiz quiz;
    @SerializedName("adobeconnect")
    @Expose
    private Adobeconnect adobeconnect;

    // --- CONSTRUCTORS ---
    public Enroll() {

    }

    public Enroll(long _0Id,
                  String username,
                  String id,
                  String courseId,
                  String courseName,
                  String categoryId,
                  String categoryName,
                  List<Teacher> teacher,
                  Integer isTeacher,
                  CourseInfo courseInfo,
                  Assign assign,
                  Quiz quiz,
                  Adobeconnect adobeconnect) {
        this._0Id = _0Id;
        this.username = username;
        this.id = id;
        this.courseId = courseId;
        this.courseName = courseName;
        this.categoryId = categoryId;
        this.categoryName = categoryName;
        this.teacher = teacher;
        this.isTeacher = isTeacher;
        this.courseInfo = courseInfo;
        this.assign = assign;
        this.quiz = quiz;
        this.adobeconnect = adobeconnect;
    }

    // --- GETTER ---
    public long get_0Id() {
        return _0Id;
    }

    public String getUsername() {
        return username;
    }

    public String getId() {
        return id;
    }

    public String getCourseId() {
        return courseId;
    }

    public String getCourseName() {
        return courseName;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public List<Teacher> getTeacher() {
        return teacher;
    }

    public Integer getIsTeacher() {
        return isTeacher;
    }

    public CourseInfo getCourseInfo() {
        return courseInfo;
    }

    public Assign getAssign() {
        return assign;
    }

    public Quiz getQuiz() {
        return quiz;
    }

    public Adobeconnect getAdobeconnect() {
        return adobeconnect;
    }

    // --- SETTER ---
    public void set_0Id(long _0Id) {
        this._0Id = _0Id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

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

    public void setCourseId(String courseId) {
        this.courseId = courseId;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public void setTeacher(List<Teacher> teacher) {
        this.teacher = teacher;
    }

    public void setIsTeacher(Integer isTeacher) {
        this.isTeacher = isTeacher;
    }

    public void setCourseInfo(CourseInfo courseInfo) {
        this.courseInfo = courseInfo;
    }

    public void setAssign(Assign assign) {
        this.assign = assign;
    }

    public void setQuiz(Quiz quiz) {
        this.quiz = quiz;
    }

    public void setAdobeconnect(Adobeconnect adobeconnect) {
        this.adobeconnect = adobeconnect;
    }
}

1 个答案:

答案 0 :(得分:1)

房间不支持直接存储列表的功能,您需要添加类型转换器。创建如下的类型转换器

public class MyCustomTypeConverter {

    Gson gson = new Gson();

    @TypeConverter
    public static List<Enroll> stringToSomeObjectList(String data) {
        if (data == null) {
            return Collections.emptyList();
        }

        Type listType = new TypeToken<List<Enroll>>() {}.getType();

        return gson.fromJson(data, listType);
    }

    @TypeConverter
    public static String someObjectListToString(List<Enroll> someObjects) {
        return gson.toJson(someObjects);
    }
}

并在实体类中注释您的字段,如下所示

@TypeConverters(MyCustomTypeConverter.class)
public final List<Enroll> someObjects;