1

The ObjectBox documentation describes creating 1:1 and 1:n relations. But is it possible and feasible to create a 1:n:m relation? So I would have a domain data model like

class ClassA {
  List<ClassB> myClassB;
}
class ClassB {
  List<ClassC> myClassC;
}
class ClassC {
  List<ClassD> myClassD;
}
class ClassD {
  int x;
}

and would want to transform this back and forth with kind of myClassB.toObjectBox() and ClassB.fromObjectBox(OboxEntity) methods.

I am not sure if I can easily extend the 1:n method to a further nested layer, and if, in general, it makes more sense to store the nested data as a JSON string.

=== UPDATE with regards to Markus's answer ===

I typically use 3 data model layers: domain, data transfer, data source. The data transfer layer is providing the intermediate methods between the other 2.

enum myEnum {left, right};

@Entity
class MyDataOboxEntity {
 int id;
 int enum;
 MyDataOboxEntity({this.id, this.enum});
}

class MyDataDtoEntity {
 int id;
 int enum; 
 MyDataDtoEntity({this.id, this.enum});
 
 factory MyDataDtoEntity.fromObjectBox(MyDataOboxEntity entity) {
    return MyDataDtoEntity(
      id: entity.id,
      enum: MyEnum[entity.enum],
  }
 
 MyDataOboxEntity toObjectBox() {
    return MyDataOboxEntity(
      id: id,
      enum: enum,
 }
 
 MyDataEntity toDomain() {
    return MyDataEntity(
      id: id,
      enum: myEnum.values[enum],
    );
}

class MyDataEntity {
 int id;
 MyEnum enum; 
 MyDataDtoEntity({this.id, this.enum});
}

and then to get a value I would call the data class in the repository with myData = MyDataDtoEntity.fromObjectBox(getDataFromObjectBox(id: id)).toDomain();

w461
  • 2,168
  • 4
  • 14
  • 40

1 Answers1

0

It's perfectly fine to have multiple relations between entity classes. ObjectBox is a database that can very efficiently lookup objects of different types.

would want to transform this back and forth with kind of myClassB.toObjectBox() and ClassB.fromObjectBox(OboxEntity) methods.

This is the part that is not very clear to me. Maybe you want to clarify?

Markus Junginger
  • 6,950
  • 31
  • 52
  • Thanks for your response. I have now explained the methods in the update of my question – w461 Sep 05 '21 at 13:56