适用于Android的OrmLite复合图案

时间:2011-11-23 22:16:54

标签: android ormlite composite foreign-collection

我正在编写Android应用程序并使用ormLite作为数据库。

我想将Composite Pattern用于某些数据库项目。

我写了以下代码:

食品

package com.android.droidfridge.data;

import com.j256.ormlite.field.DatabaseField;

public abstract class Foodstuff 
{
    @DatabaseField(id = true, useGetSet = true)
    private String name;

    @DatabaseField(useGetSet = true, foreign = true)
    private Category category;

    @DatabaseField(useGetSet = true)
    private int image;

    public String getName() 
    {
        return name;
    }

    public void setName(String name) 
    {
        this.name = name;
    }

    public Category getCategory() 
    {
        return category;
    }

    public void setCategory(Category category) 
    {
        this.category = category;
    }

    public int getImage() 
    {
        return image;
    }

    public void setImage(int image) 
    {
        this.image = image;
    }
}

成分

package com.android.droidfridge.data;

import java.io.Serializable;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "ingredients")
public class Ingredient extends Foodstuff implements Serializable
{
    private static final long serialVersionUID = 5798705059641377421L;

    @DatabaseField(useGetSet = true)
    private int amount;

    @DatabaseField(useGetSet = true)
    private int defaultAmount;

    @DatabaseField(useGetSet = true)
    private float price;

    @DatabaseField(useGetSet = true)
    private BaseUnit unit;

    public float getPrice() 
    {
        return price;
    }

    public void setPrice(float price) 
    {
        this.price = price;
    }

    public BaseUnit getUnit()
    {
        return unit;
    }

    public void setUnit(BaseUnit unit)
    {
        this.unit = unit;
    }

    public int getDefaultAmount() 
    {
        return defaultAmount;
    }

    public void setDefaultAmount(int defaultAmount)
    {
        this.defaultAmount = defaultAmount;
    }

    public int getAmount()
    {
        return amount;
    }

    public void setAmount(int amount)
    {
        this.amount = amount;
    }

    public void add(int additional)
    {
        this.amount += additional;
    }

    public void add_default_amount()
    {
        this.add(this.defaultAmount);
    }
}

配方

package com.android.droidfridge.data;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "recipes")
public class Recipe extends Foodstuff implements Serializable
{
    private static final long serialVersionUID = 635229962897104194L;

    @DatabaseField(useGetSet = true)
    private int duration;

    @ForeignCollectionField(eager = true)
    private Collection<Foodstuff> ingredients;

    public int getDuration()
    {
        return duration;
    }

    public void setDuration(int duration)
    {
        this.duration = duration;
    }

    public Collection<Foodstuff> getIngredients() 
    {
        return ingredients;
    }

    public void setIngredients(Collection<Foodstuff> ingredients) {
        this.ingredients = ingredients;
    }

    public Foodstuff getIngredient(Foodstuff ingredient) 
    {
        if(ingredients.contains(ingredient))
        {
            Iterator<Foodstuff> iterator = ingredients.iterator();
            boolean check = true;
            while(check)
            {
                if(iterator.equals(ingredient))
                    return (Foodstuff) iterator;

                if(iterator.hasNext())
                    iterator.next();
                else
                    check = false;
            }
        }

        return null;
    }

    public void addIngredient(Ingredient ingredient)
    {
        ingredients.add(ingredient);
    }

    public void removeIngredient(Foodstuff ingredient)
    {
        ingredients.remove(ingredient);
    }

    public int sizeOfIngredients()
    {
        return ingredients.size();
    }
}

分类     包com.android.droidfridge.data;

import java.io.Serializable;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "categories")
public class Category implements Serializable 
{
private static final long serialVersionUID = -2222256865380949570L;

@DatabaseField(id = true, useGetSet = true)
private String name;

public String getName() 
{
    return name;
}

public void setName(String name) 
{
    this.name = name;
}
}

食品可以是配料或食谱。食谱是成分或其他食谱。每种成分和配方都有一个类别。

我保存每个类别,成分和配方。 但是,当我启动我的应用程序时,日志显示错误如下:

11-23 21:50:16.493: I/TableUtils(261): creating table 'ingredients'
11-23 21:50:16.513: I/TableUtils(261): executed create table statement changed 1 rows: CREATE TABLE `ingredients` (`unit` VARCHAR , `defaultAmount` INTEGER , `price` FLOAT , `amount` INTEGER , `category_id` VARCHAR , `name` VARCHAR , `image` INTEGER , PRIMARY KEY (`name`) ) 
11-23 21:50:16.603: D/dalvikvm(261): GC_FOR_MALLOC freed 8647 objects / 515488 bytes in 44ms
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261): Unable to create databases, sorry dude!
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261): java.sql.SQLException: Foreign collection object class com.android.droidfridge.data.Foodstuff for field 'ingredients' does not contain a foreign field of class class com.android.droidfridge.data.Recipe
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:345)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:171)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:118)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:97)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.dao.BaseDaoImpl$3.<init>(BaseDaoImpl.java:782)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:782)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:74)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:218)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:53)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.android.droidfridge.data.DatabaseHelper.onCreate(DatabaseHelper.java:35)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:168)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:60)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:286)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.android.droidfridge.ListActivity.createIngredient(ListActivity.java:88)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.android.droidfridge.ListActivity.onCreate(ListActivity.java:45)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.view.View.performClick(View.java:2408)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.view.View$PerformClick.run(View.java:8816)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.os.Handler.handleCallback(Handler.java:587)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.os.Looper.loop(Looper.java:123)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at java.lang.reflect.Method.invoke(Method.java:521)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 21:50:16.663: E/com.android.droidfridge.data.DatabaseHelper(261):     at dalvik.system.NativeStart.main(Native Method)
11-23 21:50:16.843: I/Database(261): sqlite returned: error code = 1, msg = no such table: categories
11-23 21:50:16.843: W/System.err(261): java.sql.SQLException: Unable to run insert stmt on object com.android.droidfridge.data.Category@44ec4070: INSERT INTO `categories` (`name` ) VALUES (?)
11-23 21:50:16.853: W/System.err(261):  at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
11-23 21:50:16.853: W/System.err(261):  at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:117)
11-23 21:50:16.853: W/System.err(261):  at com.j256.ormlite.stmt.StatementExecutor.create(StatementExecutor.java:341)
11-23 21:50:16.853: W/System.err(261):  at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:288)
11-23 21:50:16.853: W/System.err(261):  at com.android.droidfridge.ListActivity.createIngredient(ListActivity.java:88)
11-23 21:50:16.853: W/System.err(261):  at com.android.droidfridge.ListActivity.onCreate(ListActivity.java:45)
11-23 21:50:16.853: W/System.err(261):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 21:50:16.853: W/System.err(261):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-23 21:50:16.853: W/System.err(261):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
11-23 21:50:16.853: W/System.err(261):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
11-23 21:50:16.863: W/System.err(261):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
11-23 21:50:16.863: W/System.err(261):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
11-23 21:50:16.863: W/System.err(261):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
11-23 21:50:16.863: W/System.err(261):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
11-23 21:50:16.863: W/System.err(261):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
11-23 21:50:16.863: W/System.err(261):  at android.view.View.performClick(View.java:2408)
11-23 21:50:16.863: W/System.err(261):  at android.view.View$PerformClick.run(View.java:8816)
11-23 21:50:16.863: W/System.err(261):  at android.os.Handler.handleCallback(Handler.java:587)
11-23 21:50:16.863: W/System.err(261):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 21:50:16.863: W/System.err(261):  at android.os.Looper.loop(Looper.java:123)
11-23 21:50:16.863: W/System.err(261):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 21:50:16.863: W/System.err(261):  at java.lang.reflect.Method.invokeNative(Native Method)
11-23 21:50:16.873: W/System.err(261):  at java.lang.reflect.Method.invoke(Method.java:521)
11-23 21:50:16.873: W/System.err(261):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 21:50:16.873: W/System.err(261):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 21:50:16.873: W/System.err(261):  at dalvik.system.NativeStart.main(Native Method)
11-23 21:50:16.873: W/System.err(261): Caused by: java.sql.SQLException: inserting to database failed: INSERT INTO `categories` (`name` ) VALUES (?)
11-23 21:50:16.883: W/System.err(261):  at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
11-23 21:50:16.883: W/System.err(261):  at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:102)
11-23 21:50:16.883: W/System.err(261):  at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:72)
11-23 21:50:16.883: W/System.err(261):  ... 24 more
11-23 21:50:16.883: W/System.err(261): Caused by: android.database.sqlite.SQLiteException: no such table: categories: , while compiling: INSERT INTO `categories` (`name` ) VALUES (?)
11-23 21:50:16.893: W/System.err(261):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-23 21:50:16.893: W/System.err(261):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
11-23 21:50:16.893: W/System.err(261):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-23 21:50:16.893: W/System.err(261):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
11-23 21:50:16.903: W/System.err(261):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
11-23 21:50:16.903: W/System.err(261):  at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
11-23 21:50:16.903: W/System.err(261):  at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:94)
11-23 21:50:16.903: W/System.err(261):  ... 25 more
11-23 21:50:16.903: I/Database(261): sqlite returned: error code = 1, msg = no such table: categories
11-23 21:50:16.903: I/Database(261): [ 11-23 21:50:17.044   261:0x105 I/java.sql.SQLException: queryForOne from database failed: SELECT * FROM `categories` WHERE `nObject Error
11-23 21:50:17.044: I/Database(261): sqlite returned: error code = 1, msg = no such table: categories
11-23 21:50:17.044: W/System.err(261): java.sql.SQLException: Unable to run insert stmt on object com.android.droidfridge.data.Category@44ee01b8: INSERT INTO `categories` (`name` ) VALUES (?)
11-23 21:50:17.053: W/System.err(261):  at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
11-23 21:50:17.053: W/System.err(261):  at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:117)
11-23 21:50:17.053: W/System.err(261):  at com.j256.ormlite.stmt.StatementExecutor.create(StatementExecutor.java:341)
11-23 21:50:17.053: W/System.err(261):  at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:288)
11-23 21:50:17.053: W/System.err(261):  at com.android.droidfridge.ListActivity.createIngredient(ListActivity.java:88)
11-23 21:50:17.053: W/System.err(261):  at com.android.droidfridge.ListActivity.onCreate(ListActivity.java:46)
11-23 21:50:17.053: W/System.err(261):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 21:50:17.053: W/System.err(261):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-23 21:50:17.053: W/System.err(261):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
11-23 21:50:17.053: W/System.err(261):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
11-23 21:50:17.053: W/System.err(261):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
11-23 21:50:17.053: W/System.err(261):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
11-23 21:50:17.053: W/System.err(261):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
11-23 21:50:17.053: W/System.err(261):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
11-23 21:50:17.053: W/System.err(261):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
11-23 21:50:17.053: W/System.err(261):  at android.view.View.performClick(View.java:2408)
11-23 21:50:17.053: W/System.err(261):  at android.view.View$PerformClick.run(View.java:8816)
11-23 21:50:17.053: W/System.err(261):  at android.os.Handler.handleCallback(Handler.java:587)
11-23 21:50:17.053: W/System.err(261):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 21:50:17.053: W/System.err(261):  at android.os.Looper.loop(Looper.java:123)
11-23 21:50:17.053: W/System.err(261):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-23 21:50:17.053: W/System.err(261):  at java.lang.reflect.Method.invokeNative(Native Method)
11-23 21:50:17.053: W/System.err(261):  at java.lang.reflect.Method.invoke(Method.java:521)
11-23 21:50:17.053: W/System.err(261):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-23 21:50:17.053: W/System.err(261):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-23 21:50:17.053: W/System.err(261):  at dalvik.system.NativeStart.main(Native Method)
11-23 21:50:17.053: W/System.err(261): Caused by: java.sql.SQLException: inserting to database failed: INSERT INTO `categories` (`name` ) VALUES (?)
11-23 21:50:17.053: W/System.err(261):  at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
11-23 21:50:17.053: W/System.err(261):  at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:102)
11-23 21:50:17.053: W/System.err(261):  at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:72)
11-23 21:50:17.053: W/System.err(261):  ... 24 more
11-23 21:50:17.053: W/System.err(261): Caused by: android.database.sqlite.SQLiteException: no such table: categories: , while compiling: INSERT INTO `categories` (`name` ) VALUES (?)
11-23 21:50:17.073: W/System.err(261):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-23 21:50:17.073: W/System.err(261):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
11-23 21:50:17.073: W/System.err(261):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-23 21:50:17.073: W/System.err(261):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
11-23 21:50:17.073: W/System.err(261):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
11-23 21:50:17.073: W/System.err(261):  at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
11-23 21:50:17.073: W/System.err(261):  at com.j256.ormlite.android.AndroidDatabaseConnection.insert(AndroidDatabaseConnection.java:94)
11-23 21:50:17.073: W/System.err(261):  ... 25 more
11-23 21:50:17.073: I/Database(261): sqlite returned: error code = 1, msg = no such table: categories
11-23 21:50:17.073: I/Database(261): [ 11-23 21:50:17.083   261:0x105 I/java.sql.SQLException: queryForOne from database failed: SELECT * FROM `categories` WHERE `nObject Error
11-23 21:50:17.083: I/Database(261): sqlite returned: error code = 1, msg = no such table: categories
11-23 21:50:17.083: E/Recipe(261): java.sql.SQLException: queryForOne from database failed: SELECT * FROM `categories` WHERE `name` = ?

问题

  • 为什么我不能创建类别数据库?
  • 在配料属性中我的食谱类是否有错误?

1 个答案:

答案 0 :(得分:1)

ORMLite正在记录的错误正在尝试提供帮助您解决问题的信息:

SQLException: Foreign collection object class com.android.droidfridge.data.Foodstuff
    for field 'ingredients' does not contain a foreign field of class
    class com.android.droidfridge.data.Recipe

您的Recipe课程包含以下字段:

@ForeignCollectionField(eager = true)
private Collection<Foodstuff> ingredients;

如果您的食谱中含有成分,那么Foodstuff中必须有一个外国字段,用于拥有它的Recipe。这允许ORMLite执行以下操作:

SELECT * FROM foodstuff WHERE recipe_id = id;

例如,如果您的Account具有多个Order s,则每个Order对象都应具有外部Account字段。以下是foreign collections的文档。

相反,如果你想要一个食谱包括其他食谱将要使用的成分,那么我认为你需要的是一个“连接表”而不是一个外国集合。看一下使用连接表的"Many To Many" example