【问题标题】:ORMLite collection of entities without referenceORMLite 无参考实体集合
【发布时间】:2015-01-23 12:35:53
【问题描述】:

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

有人能指点我笑一下,告诉我为什么我做错了,我在这里误解了什么。谢谢。

【问题讨论】:

    标签: android ormlite foreign-collection


    【解决方案1】:

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

    请记住,当您有一个 ForeignCollection 字段时,集合中的类(在此示例中为 Order)必须具有一个用于具有该集合的类的外部字段(在此示例中为 Account)。如果Account 有一个Orders 的外部集合,那么Order 必须有一个Account 外部字段。它是必需的,以便 ORMLite 可以找到与特定帐户匹配的订单。

    在您的示例中,要让ORMLite 确定特定News 实体具有哪个AttachmentAttachment 实体必须具有News 字段。另一种方法是创建一个连接表,但 ORMLite 不会为您这样做。

    【讨论】:

    • 好的,所以我只需要创建一个连接表。我会这样做,我希望后面会创建一些隐藏的表格。感谢您的回复。
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多