I have these classes:
public class AllegationContent {
@DatabaseField(generatedId = true)
protected long id;
@DatabaseField(canBeNull = true, foreign = true)
private AllegationItem allegationItem;
@DatabaseField()
protected String content;
...
}
And:
public class AllegationItem {
@DatabaseField(generatedId = true)
protected long id;
@ForeignCollectionField(eager = false)
protected ForeignCollection<AllegationContent> allegationContent;
...
}
How can I persist both?
I am trying this:
allegationContentDao = getAllegationContentDao();
AllegationContent allegationContent = new AllegationContent();
allegationContent.setContent("allegation content");
allegationContentDao.create(allegationContent);
allegationItemDao = getAllegationItemDao();
AllegationItem allegationItem = new AllegationItem();
AllegationContent allegationContent2 = allegationContentDao.queryForSameId(allegationContent); //is that wrong?
allegationItem.getAllegationContent().add(allegationContent2);
allegationItemDao.create(allegationItem);
but I got this error:
04-24 10:41:57.128: ERROR/AndroidRuntime(802): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.project/br.com.project.DaoTestActivity}: java.lang.NullPointerException
I solved. The table can’t create because don’t have atributes, only id (what’s another problem). The NPE happen because getAllegationContent return null, so first I need create a ForeignCollection list, set using setAllegationContent and then get.