2

Objectbox 是否支持具有多个多对多关系 (M:N) ? 例如:

// MIX with @Backlink and no @Backlink --> causes Error in this class
@Entity()
class Exercise {
int id;
String title;
String description;

final unitTypes = ToMany<UnitType>();

// Many To Many with Equipment
@Backlink()
final equipments = ToMany<Equipment>();

... Constructor etc.
}

// NO mix with @Backlink and no @Backlink --> no Error in this class
@Entity()
class UnitType{
 int id;
 String title;
 
 @Backlink()
 final exercises = ToMany<Exercises>();

 @Backlink()
 final units = ToMany<Unit>();

 ... Constructor etc.
}

@Entity()
class Unit{
 int id;
 String title;
 String short;

 final unitType = ToOne<UnitType>();

 ... Constructor etc.
}

就我的测试而言,我在一个类中使用多个多对多关系尝试的所有构建都失败了。抛出类似这样的错误:

could not format because the source could not be parsed:

line 1078, column 212 of .: Expected to find '}'.
     ╷
1078 │         toManyRelations: (Exercise object) => {RelInfo<Exercise>.toMany(27, object.id): object.unitTypesRelInfo<ExerciseEquipment>.toOneBacklink(3, object.id, (ExerciseEquipment srcObject) => srcObject.exercise): object.exerciseEquipments,

那么有没有办法在 ObjectBox 中完成这项工作,或者我是否必须使用另一个具有一对多关系的 Binder 类,例如在 Exercise 和 UnitType 之间 --> ExerciseUnitType (这个解决方法对我有用,但是不漂亮,并且显着增加了对额外类和额外存储数据的需求

@Entity()
class ExerciseUnitType{
 int id;
 String title;
 
 @Backlink()
 final exercise= ToOne<Exercise>(); 

 @Backlink()
 final unitType = ToOne<UnitType>();

 ... Constructor etc.
}
4

1 回答 1

2
could not format because the source could not be parsed:

line 1078, column 212 of .: Expected to find '}'.
     ╷
1078 │         toManyRelations: (Exercise object) => {RelInfo<Exercise>.toMany(27, object.id): object.unitTypesRelInfo<ExerciseEquipment>.toOneBacklink(3, object.id, (ExerciseEquipment srcObject) => srcObject.exercise): object.exerciseEquipments,

是 objectbox-generator 代码中的错误 - 它为您的配置创建了语法错误的代码(缺少逗号)。显然,在同一实体中同时拥有链接和反向链接并不是集成测试的一部分。我已经解决了这个问题,您可以更新您的 pubspec.yaml 以临时使用来自 github 的最新 objectbox-generator(使用 a dependency_override),或者等到修复在下一个 ObjectBox 版本中推出。要使用 Git 的固定版本:只需在 pubspec.yaml 末尾添加以下代码

dependency_overrides:
  objectbox_generator:
    git:
      url: https://github.com/objectbox/objectbox-dart.git
      ref: 461a948439dcc42f3956b7d21b232eb9c2bc26e1
      path: generator
于 2021-07-19T12:39:40.103 回答