没有引用的ORMLite实体集合

时间:2015-01-23 12:35:53

标签: android ormlite foreign-collection

我想添加一个Attachment实体,我将从多个不同的实体中引用它,但它没有引用这些,我如何在ORMLite中使用它?

我一直得到这个例外:

 Caused by: java.sql.SQLException: Foreign collection class entity.Attachment for
    field 'attachments' column-name does not contain a foreign field named
    'attachmentId' of class enity.News

例如,我有一个新闻实体

@DatabaseTable
public class News extends Record {

    @DatabaseField(index = true, id = true)
    private long newsArticleId;
    @DatabaseField
    private String subject;
    @DatabaseField
    private String content;

    @ForeignCollectionField
    Collection<Attachment> attachments;
}

附件实体:

@DatabaseTable
public class Attachment extends Record {

    @DatabaseField(id = true, index = true)
    private long attachmentId;

    @DatabaseField
    private String attachmentUrl;
}

有人可以指着我笑,告诉我为什么我这样做错了,我在这里误会了。感谢。

1 个答案:

答案 0 :(得分:1)

这是常见问题解答。引用ORMLite docs on foreign-collections

  

请记住,当您拥有ForeignCollection字段时,集合中的类必须(在此示例中为Order)必须具有包含该集合的类的外部字段(在此示例中为Account) 。如果Account的外国收藏集为Orders,则Order必须包含Account外地字段。这是必需的,因此ORMLite可以找到与特定帐户匹配的订单。

在您的示例中,要ORMLite确定特定Attachment实体所具有的NewsAttachment实体必须具有News字段。另一种方法是建立联接表,但ORMLite不会为你做这件事。

相关问题