坚持双向关系ormlite

时间:2012-04-24 13:46:39

标签: android ormlite

我有这些课程:

public class AllegationContent {

    @DatabaseField(generatedId = true)
    protected long id;

    @DatabaseField(canBeNull = true, foreign = true)
    private AllegationItem allegationItem;

    @DatabaseField()
    protected String content;
    ...
}

public class AllegationItem {
   @DatabaseField(generatedId = true)
    protected long id;

    @ForeignCollectionField(eager = false)
    protected ForeignCollection<AllegationContent> allegationContent;
    ...
}

我怎样才能坚持下去? 我正在尝试这个:

    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);

但是我收到了这个错误:

  

04-24 10:41:57.128:ERROR / AndroidRuntime(802):java.lang.RuntimeException:无法启动活动ComponentInfo {br.com.project / br.com.project.DaoTestActivity}:java.lang。的NullPointerException

1 个答案:

答案 0 :(得分:1)

我解决了。该表无法创建,因为没有属性,只有id(什么是另一个问题)。 NPE的发生是因为getAllegationContent返回null,所以首先我需要创建一个ForeignCollection列表,使用setAllegationContent设置然后获取。