当前位置: 首页 > 知识库问答 >
问题:

Android-错误:无法确定如何将此字段保存到数据库中。你可以考虑为它添加一个类型转换器吗?

艾志尚
2023-03-14

我有以下型号:

@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:无法找出如何将此字段保存到数据库中。可以考虑为它添加一个类型转换器:<代码>波纹线:

@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;
    }
}

列表

@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个答案

乐正涵忍
2023-03-14

文件室不支持直接存储列表的功能,您需要添加类型转换器来实现这一点。创建一个TypeConverter,如下所示

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;
 类似资料: