Search code examples
androidentitygreendao

GreenDao list entity with a list of entities


I am making an Android application using GreenDao, and I have these two entities:

@Entity
public class Quiz {

    @Id(autoincrement = true)
    private Long id;

    private Date date;

    private String type;

    @ToMany(referencedJoinProperty = "quizId")
    private List<Answer> answers;
}

@Entity
public class Answer {

    @Id(autoincrement = true)
    private Long id;

    private int answer;

    private float value;

    private int questionNumber;

    private String type;

    private Long quizId;
}

I'm trying to fetch the list of Quiz using the following code:

DaoSession daoSession = AndroidAdapter.getDaoSession();
QuizDao quizDao = daoSession.getQuizDao();
List<Quiz> quizs = quizDao.loadAll();

But the list of answers always comes empty, what am I doing wrong?


Solution

  • My AndroidAdapter class:

    public class AndroidAdapter {
    
        public static Context getContext() {
            return AppApplication.getContext();
        }
    
        public static DaoSession getDaoSession() {
            return AppApplication.getDaoSession();
        }
    }
    

    And AppApplication class

    @Override
    public void onCreate() {
        super.onCreate();
    
        context = this;
    
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,ENCRYPTED ? "exam-db-encrypted" : "exam-db");
        Database db = ENCRYPTED ? helper.getEncryptedWritableDb("exam-secret") : helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    
        this.addAllEvents();
    }
    
    public static DaoSession getDaoSession() {
        return daoSession;
    }