2015-06-07 116 views
0

我使用糖ORM 1.3.1,我試圖挽救以下對象糖ORM犯規存儲數據

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()我的清單並沒有保存。我做錯了什麼或它不保存列表?

+0

添加記錄創建的示例。你可能有一個錯字,或者引用錯誤的對象等,但我們看不到。我還將包括完整模型(構造函數和getters/setters)。 –

+0

只需一分鐘,我會更新整個模型 – ask110593

+0

剛剛添加完整的文章模型,列表它不存儲 – ask110593

回答

1

我有幾件事要看。

首先。我不相信數據庫將處理一個List類型或其他Object類型的列。當我試着運行你的代碼時,我收到了:

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

這些應該是數據庫可以存儲的類型。嘗試序列化您的列表到字符串以將它們插入到數據庫中。你可以嘗試在你的getter和setter中做這個。 setter獲取對象列表並將其轉換爲序列化的String,getter接受序列化的String並將其轉換回對象列表。

在SugarOrm的情況下,你可以參考其它表,它會創建一個關係這裏概述:

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

公告的「作者」型只是在數據庫中引用的記錄,從另一個表。

我通常嘗試並在我的默認構造函數中設置我的默認值。所以像這樣:

... property declarations... 

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

希望這可以幫助你完成troubleblshooting!