使用Foreign Collection Field创建表 [英] Create table with Foreign Collection Field

查看:145
本文介绍了使用Foreign Collection Field创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个抽象类:

DomainItem

DomainItem

abstract public class DomainItem {

    @DatabaseField(generatedId = true)
    protected long id;

    @ForeignCollectionField(eager = false)
        protected ForeignCollection<ContentItem> contentItens;

    //getters and setters
}

ContentItem:

ContentItem:

abstract public class ContentItem {

    @DatabaseField(generatedId = true)
    protected long id;

    @DatabaseField(foreign = true)
    protected DomainItem domainItem;


    @DatabaseField()
    protected String content;

    //getters and setters
}

这些(没有抽象):

@DatabaseTable()
public class PhytoterapicItem extends DomainItem{

    public PhytoterapicItem(){

    }

}

PhytoterapicContent

PhytoterapicContent

@DatabaseTable(tableName = "phytoterapiccontent")
public class PhytoterapicContent extends ContentItem {

    @DatabaseField(canBeNull = false)
    private String defaultName;

    @DatabaseField(canBeNull = false)
    private String scientificName;

    //getters and setters
}

在我的DatabaseHelper中我尝试创建表:

In my DatabaseHelper I trying create the tables:

//DatabaseHelper
...
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    try {
        Log.i(TAG, "onCreate");
        TableUtils.createTable(connectionSource, PhytoterapicContent.class);
        Log.i(TAG, "Created table PhytoterapicContent");

        TableUtils.createTable(connectionSource, PhytoterapicItem.class);
        Log.i(TAG, "Created table PhytoterapicItem");
    catch{
       ...
    }

表PhytoterapicContent被建造。但是我得到了以下错误:

The table PhytoterapicContent is created. But I got the follow error:


java.sql.SQLException:外部集合类br.com.project.model.ContentItem for field' contentItens'column-name不包含类br.com.project.model.PhytoterapicItem的外部字段

java.sql.SQLException: Foreign collection class br.com.project.model.ContentItem for field 'contentItens' column-name does not contain a foreign field of class br.com.project.model.PhytoterapicItem


推荐答案

因此异常消息旨在帮助这里。引用删除的类名称:

So the exception message was designed to help here. To quote it with the class names removed:


外地集合类 ContentItem for field contentItens column-name不包含类的外部字段 PhytoterapicItem

Foreign collection class ContentItem for field contentItens column-name does not contain a foreign field of class PhytoterapicItem

看起来就是这样。每当你有 ForeignCollection 时,集合包含的类必须将一个外部字段返回给父类。

That looks to be the case. Whenever you have ForeignCollection, the class contained by the collection must have a foreign field back to the parent class.

在您的情况下, PhytoterapicItem 扩展 DomainItem 类,其中 ForeignCollection of ContentItem 对象。这意味着 ContentItem 必须具有 PhytoterapicItem 类型的外部字段。否则, ORMLite 如何知道表中的哪些 ContentItem 项目与特别是 PhytoterapicItem

In your case, PhytoterapicItem extends the DomainItem class which has a ForeignCollection of ContentItem objects. That means that ContentItem must have a foreign field of type PhytoterapicItem. Otherwise, how would ORMLite know which of the ContentItem items in the table are associated with a particular PhytoterapicItem.

帐户和<的例子code>订单 外国馆藏文档中的对象可以帮助您了解架构。每个帐户都有一个订单对象的外国集合。每当您查询帐户时,将执行单独的查询以查找与特定<对应的 Order 对象的集合code>帐户。这意味着每个订单必须有一个外国账户对象。

The example of Account and Order objects in the foreign collection documentation may help you with your schema. Each Account has a foreign collection of Order objects. Whenever you query for an Account a separate query is performed to find the collection of Order objects that correspond to a particular Account. That means that each Order has to have a foreign Account object.

这篇关于使用Foreign Collection Field创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆