Android如何在ORMLite中建立自我关系?

时间:2012-12-19 21:27:08

标签: android database foreign-keys ormlite

我有以下对象是自我关系,如下所示:

@DatabaseTable(tableName = "categoria")
public class Categoria implements Serializable {

    @DatabaseField(generatedId = true)
    public int id;

    @DatabaseField(canBeNull = true, foreign = true)
    public Categoria pai;

    @DatabaseField(canBeNull = false, width = 50, unique = true)
    public String descricao;

    public Categoria() { }
}

当我这样做时:

Categoria cat = new Categoria();
cat.pai = null;
cat.descricao = "Comidas";

categoryDao.create(cat); //should be id = 1.

Categoria sub_cat = new Categoria();
sub_cat.pai = cat;
sub_cat.descricao = "Bebidas";

categoryDao.create(sub_cat); // should be id = 2.

所以当我这样做时,我的对象Categoria为null:

Categoria sub_cat = categoryDao.queryForId(2);

sub_cat.descricao // is ok, return "Bebidas".

sub_cat.pai // is null when supposed to be sub_cat.pai.id = 1.

我做错了什么?

0 个答案:

没有答案