Does Realm for Java allows nested custom Class with map?

74 views
Skip to first unread message

Kent C

unread,
Nov 21, 2015, 3:36:42 AM11/21/15
to Realm Java
Does Realm for Java allows nested custom Class?  I am trying to figure out how to use realm for the Directory class which has Map<String, School>.

Is the following class possible to convert to realm.  If not, is there any alternative to use realm for the following?

I can see the potential of realm, and I hope I will be able to learn from it.  Thanks.


My Sample data as followed:

school id: A1, "Point Grey"
Student name: "Kent", "Age": 43
Student name: "Jane", "Age": 18
Student name: "Winnie", "Age": 19 

school id: B1, "Prince of Wales"
Student name: "Steven", "Age": 11
Student name: "Michelle", "Age": 30
Student name: "Ronald", "Age": 20

My Directory Class is as followed:


    public class DIRECTORY {

    private Map<String, SCHOOL> schools = new HashMap<String, SCHOOL>();


    public void setSchools(Map<String, SCHOOL> schoolsMap) {
        this.schools =  schoolsMap;
    }

    public Map<String, SCHOOL> getSchools() {return this.schools;}


    public void addSchool(String schoolId, String schoolName) {
        SCHOOL school = new SCHOOL();
        school.setSchoolId(schoolId);
        school.setSchoolName(schoolName);
        schools.put(schoolId,school);

    }

    public SCHOOL getSchoolById(String schoolId) {
        return schools.get(schoolId);
    }
    }

My Class for School as followed:

    public class SCHOOL {

    private String schoolId;
    private String schoolName;
    private List<STUDENT> students = new ArrayList<>();

    //Getter and setter for schooled and schoolname


    public void setStudents(List<STUDENT> students) {
        this.students = students;
    }


    public List<STUDENT> getStudents() {return this.students;}


    public void addStudent(String names, String age) {
        STUDENT student = new STUDENT();
        student.setNames(names);
        student.setAge(age);
        students.add(student);
     }
    }

My student Class as followed:

    public class STUDENT {

    private String names;
    private String age;
   

    //getters and setters for names and age
    }

Christian Melchior

unread,
Nov 23, 2015, 1:12:49 AM11/23/15
to Kent C, realm...@googlegroups.com
Hi Kent

Realm does allow nested RealmObjects and RealmLists, but we unfortunately doesn't support Map or Set yet. You can follow this issue on progress for that:
https://github.com/realm/realm-java/issues/759

Right now the best work-around is probably using a RealmList<School> and then use queries instead of keys:

RealmList<School> schools;
School school = schools.where().equalTo("id", schoolId).findFirst();

Best,

--
Christian Melchior
Senior Android Developer
{#HS:141183286-2311#}
--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/66fb3c03-5c04-46d3-9788-2add5da13f58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages