Sugar ORM 不存储数据

Sugar ORM doesnt store data

我正在使用 Sugar ORM 1.3.1,我正在尝试保存以下对象

   public class Article extends SugarRecord<Article> implements Serializable {
    @JsonProperty("Categories")
    private List<Category> categories = new ArrayList<Category>();
    @JsonProperty("Contents")
    private List<Content> contents = new ArrayList<Content>();
    @JsonProperty("Country")
    private CountryRelated country;
    @JsonProperty("Description")
    private String description;
    @JsonProperty("ExpiryDate")
    private String expiryDate;
    @JsonProperty("ExtraFields")
    private List<ExtraField> extraFields = new ArrayList<ExtraField>();
    @JsonProperty("Identifier")
    private int identifier;
    @JsonProperty("ImageURL")
    private String imageURL;
    @JsonProperty("Name")
    private String name;
    @JsonProperty("PortalID")
    private int portalID;
    @JsonProperty("PublishDate")
    private String publishDate;
    @JsonProperty("Region")
    private Region region;
    @JsonProperty("Related")
    private List<Related> related = new ArrayList<Related>();
    @JsonProperty("Newsbite")
    private boolean newsbite;
    @JsonProperty("ShareURL")
    private String shareURL;
    @JsonProperty("Tags")
    private List<Tag> tags = new ArrayList<Tag>();
    @JsonProperty("Type")
    private int type;
    public Article() {
    }


    public Article(List<Category> categories, List<Content> contents, List<ExtraField> extraFields, CountryRelated country, String description, String expiryDate, int identifier, String imageURL, String name, boolean newsbite, int portalID, String publishDate, Region region, List<Related> related, String shareURL, List<Tag> tags, int type) {
        this.categories = categories;
        this.contents = contents;
        this.extraFields = extraFields;
        this.country = country;
        this.description = description;
        this.expiryDate = expiryDate;
        this.identifier = identifier;
        this.imageURL = imageURL;
        this.name = name;
        this.newsbite = newsbite;
        this.portalID = portalID;
        this.publishDate = publishDate;
        this.region = region;
        this.related = related;
        this.shareURL = shareURL;
        this.tags = tags;
        this.type = type;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public List<Content> getContents() {
        return contents;
    }

    public void setContents(List<Content> contents) {
        this.contents = contents;
    }

    public List<ExtraField> getExtraFields() {
        return extraFields;
    }

    public void setExtraFields(List<ExtraField> extraFields) {
        this.extraFields = extraFields;
    }

    public CountryRelated getCountry() {
        return country;
    }

    public void setCountry(CountryRelated country) {
        this.country = country;
    }

    public String getDescription() {
        return description;
    }

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

    public String getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(String expiryDate) {
        this.expiryDate = expiryDate;
    }

    public int getIdentifier() {
        return identifier;
    }

    public void setIdentifier(int identifier) {
        this.identifier = identifier;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isNewsbite() {
        return newsbite;
    }

    public void setNewsbite(boolean newsbite) {
        this.newsbite = newsbite;
    }

    public int getPortalID() {
        return portalID;
    }

    public void setPortalID(int portalID) {
        this.portalID = portalID;
    }

    public String getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(String publishDate) {
        this.publishDate = publishDate;
    }

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    public List<Related> getRelated() {
        return related;
    }

    public void setRelated(List<Related> related) {
        this.related = related;
    }

    public String getShareURL() {
        return shareURL;
    }

    public void setShareURL(String shareURL) {
        this.shareURL = shareURL;
    }

    public List<Tag> getTags() {
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}

Category.class

public class Category extends SugarRecord<Category> implements Serializable {
    @JsonProperty("Identifier")
    private int identifier;
    @JsonProperty("Name")
    private String name;
public constructor + getters and setters }

Content.class 和 CountryRelated.class 具有相同的结构

在我执行 article.save() 之后我的列表没有保存。我是做错了什么还是根本不保存列表???

我看的东西不多。

第一。我不相信数据库会处理具有列表类型或其他对象类型的列。当我尝试 运行 你的代码时,我收到:

Class cannot be read from Sqlite3 database. Please check the type of field categories(java.util.List)

这些应该是数据库可以存储的类型。尝试将您的列表序列化为字符串以将它们插入数据库。您可以在 getter 和 setter 中尝试这样做。 setter 获取对象列表并将其转换为序列化字符串,getter 获取序列化字符串并将其转换回对象列表。

在 SugarOrm 实例中,您可以引用另一个 table,它将创建此处概述的关系:

http://satyan.github.io/sugar/creation.html#why

注意 "Author" 类型只是引用数据库中另一个 table 的记录。

第二 我通常会尝试在我的默认构造函数中设置我的默认值。所以像:

... property declarations...

public Article() {
    this.name = "Default Value";
}

希望这能帮助您完成故障排除!