没有引用的 ORMLite 实体集合

ORMLite collection of entities without reference

我想添加一个附件实体,我将从多个不同的实体引用它,但它不会引用这些实体,我如何在 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;
}

有人可以指着我笑着告诉我为什么我做错了以及我在这里误解了什么。谢谢

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

Remember that when you have a ForeignCollection field, the class in the collection must (in this example Order) must have a foreign field for the class that has the collection (in this example Account). If Account has a foreign collection of Orders, then Order must have an Account foreign field. It is required so ORMLite can find the orders that match a particular account.

在您的示例中,为了 ORMLite 确定特定 News 实体具有哪些 AttachmentAttachment 实体必须具有 News 字段.另一种方法是加入 table,但 ORMLite 不会为您这样做。