ForeignCollection is setting as null

64 views
Skip to first unread message

Surojit Pakira

unread,
Apr 18, 2015, 7:32:46 AM4/18/15
to ormlite...@googlegroups.com
Hello Gray,

I was trying to fetch the ForeignCollection but unfortunately getting null value. Please find the details below:

Class StudentDetails
{

@DatabaseField(generatedId = true, columnName = "student_id")
public int studentId;

@DatabaseField(canBeNull = false, foreign = true, foreignAutoRefresh = true)
public TeacherDetails teacher;

.....
}

--------------------------------------

class TeacherDetails
{

@DatabaseField(generatedId = true, columnName = "teacher_id")
public int teacherId;

@ForeignCollectionField(eager = true, maxEagerLevel = 2)
    private ForeignCollection<StudentDetails> students;


public ForeignCollection<StudentDetails> getStudents(){
return this.students;
}

}


-----------------------------------------------------------------------------------------------

I have added the reference of TeacherDetails in StudentDetails class while inserting into the database and can see "teacher" field of "StudentDetails" is being populated with actual teacherId.

But, when I am trying to query a teacher along with it's associated students, the getStudents() is always sending "null". Code which I am using is below :



TeacherDetails tDetails = getHelper().getTeacherDao().queryForId(1);
ForeignCollection<StudentDetails> students = tDetails.getStudents();                       // students  is always set as null

Am I missing something ?


- Surojit




Emanuel Salgado

unread,
Jan 5, 2016, 8:25:50 AM1/5/16
to ORMLite Android Users
I believe you should create a new class StudentTeacher as follows:
 
class StudentTeacher
{

@DatabaseField(generatedId = true)
private int _id;

@DatabaseField(foreign = true, foreignAutoRefresh = true)
private Student student;

@DatabaseField(foreign = true, foreignAutoRefresh = true)
private Teacher teacher;

...
}

The TeacherDetails class must be edited to:

class TeacherDetails
{

@DatabaseField(generatedId = true, columnName = "teacher_id")
public int teacherId;

@ForeignCollectionField(eager = true, maxEagerLevel = 2)
    private ForeignCollection<StudentTeacher> studentTeacher;

...
}

You must create a method for get only the StudentDetails of studentTeacher attribute in the Student class.
 
Reply all
Reply to author
Forward
0 new messages