ORMLite - 外来对象

时间:2014-03-25 15:24:56

标签: orm ormlite foreign-collection

如何使用ORMLite正确使用Foreign?

我有一个类货币对,它由2类货币实例组成。 我明白,DB不会保存实例,而是保存ID。

这些类的相关部分。

DatabaseTable(tableName = "CurrencyPairs")
public class CurrencyPair {
    @DatabaseField(id = true)
    private int id;
    @DatabaseField(columnName = "baseCurrency_ID", **foreign** = true )
    private CCC_Currency baseCurrency;
    @DatabaseField(columnName = "quotedCurrency_ID", **foreign** = true )
    private CCC_Currency quotedCurrency;    
    @DatabaseField(columnName = "subscribed")
    private boolean subscribed;

@DatabaseTable(tableName = "CCC_Currencies")
public class CCC_Currency {

    @DatabaseField(columnName = "currencyName")
    private String currencyName;
    @DatabaseField(columnName = "shortName")
    private String shortName;
    @DatabaseField(columnName = "symbol")
    private String symbol;
    @DatabaseField(columnName = "isFiat")
    private boolean isFiat;

    @DatabaseField(generatedId = true)
    private int id;

我现在用

加载Currencypairs
    try {
        CurrencyPair_Dao = DaoManager.createDao(dbHandler.getInstance(),
                CurrencyPair.class);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Get Objects and add them to the library
    List<CurrencyPair> all = null;
    try {
        all = CurrencyPair_Dao.queryForAll();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

它在列表中创建currencyPairs all,但它们是字段(示例baseCurrency)为null。 不知何故,ID并没有链接到对象本身。

我缺少什么?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

首先,您必须保留一个CCC_Concurrency实例,因此OrmLite将设置生成的ID。然后在ConcurrencyPair的对象上设置此实例,并将其保留。