Dave
Dave

Reputation: 141

Realm : How to structure one to many relationship

I have been looking over to many ORM's in android, by far what i have used and would fit to my app so far are ActiveAndroid and SugarOrm, but what i need is not currently supported(not supported but can be fix by creating sql scripts) as of now (one-to-many relationship). Im looking at Realm ORM for android a very promising one.

Is this possible with Realm?

// this is just a sample of what i need to do.,
// parent
class Message{
  long id;
  List<Meta> messages;
}

// child
class Meta{
  long senderId;
  String message;
  Date date;
  int status;
}

// I have already know how to do this on ActiveAndroid but seems a bit hard
// to update records or fetch single data.

Note: I have been having problems lately when manually creating my SQL scripts, and its very time consuming coding all of those when ORM's are there, And its very annoying when something needs to change I have to restructure most of the affected columns and etc.

I hope I had asked my question clearly and I hope there is a better way for this.

Thanks

Upvotes: 1

Views: 5016

Answers (1)

Christian Melchior
Christian Melchior

Reputation: 20126

Yes, that is very much possible with Realm. Realm is an object store, so it stores your objects as they are. In a normal object model one-to-many relationships are defined using lists, the same with Realm which has a special list called RealmList you can use.

In Realm the model you have should be defined as follow:

class Message extends RealmObject {
  private long id;
  private RealmList<Meta> messages;

  // Getters and setters
}

class Meta extends RealmObject {
  private long senderId;
  private String message;
  private Date date;
  private int status;

  // Getters and setters
}

Upvotes: 6

Related Questions