Android How to make self-relationship in ORMLite?

北慕城南 提交于 2019-12-24 20:42:03

问题


I have the following object that is a self-relationship, as follows:

@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() { }
}

When I do this:

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.

So my object Categoria is null when do this:

Categoria sub_cat = categoryDao.queryForId(2);

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

but

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

What I'm doing wrong?

来源:https://stackoverflow.com/questions/13961470/android-how-to-make-self-relationship-in-ormlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!