Hi José
Realm currently doesn't support composite keys, so right now the workaround would be to create a calculated key you manually keep up to date whenever you change any of the composite key values:
public MyObj extends RealmObject {
@PrimaryKey
private String compositeKey;
private int int1;
private int int2;
// Getters and setters
public static String getCompositeKey(MyObj obj) {
return Integer.toString(obj.getInt1()) + Integer.toString(obj.getInt2());
}
}
realm.beginTransaction();
myObj.setInt1(42);
myObj.setInt2(43);
myObj.setCompositeKey(MyObj.getCompositeKey(myObj));
realm.commitTransaction();
This is not ideal and is something we want to fix. You can follow progress on that here:
https://github.com/realm/realm-java/issues/1129Regarding many-to-many relations, then you have to keep in mind that Realm is not a relational database, so the standard way of having to create a many-to-many table doesn't apply here. Instead you can just do the following:
public class Foo extends RealmObject {
private RealmList<Bar> bars;
public void setBars(ReamList<Bar> bars) {
this.bars = bars;
}
public RealmList<Bar> getBars() {
return bars;
}
}
public class Bar extends RealmObject {
private RealmList<Foo> foos;
public void setFoos(ReamList<Foo> foos) {
this.foos = foos;
}
public RealmList<Foo> getFoos() {
return foos;
}
}
I hope that can get you started.
--
Christian Melchior
Senior Android Developer