问题
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