is it possible to have two or more ToMany relations pointing to the same object class?

66 views Asked by At

I am trying to establish a relation between objects like the one below. After adding different Order objects to mainCourses and dessert, when loading the Customer object, both mainCourses and dessert lists have the same values.

@Entity()
class Customer {

int id = 0;

@Backlink('customer')
final mainCourses = ToMany<Order>();

@Backlink('customer')
final dessert = ToMany<Order>();
}

@Entity()
class Order {
int id = 0;
final customer = ToOne<Customer>();
}

I expected that each ToMany relation could contain its own list of Order objects

1

There are 1 answers

0
Uwe - ObjectBox On

The ToMany relations are set up as @Backlink to the same customer ToOne relation in Order. Backlink relations do not really exist, they are based on another relation.

You could create another ToOne in Order just for desserts:

// In Customer
@Backlink('customerDessert')
final dessert = ToMany<Order>();

// In Order
final customerDessert = ToOne<Customer>();

Or create an actual ToMany relation for each:

// In Customer
final mainCourses = ToMany<Order>();
final dessert = ToMany<Order>();

// In Order remove the ToOne