When creating entities (for inserting into ObjectBox), the ToMany field will throw this error if I use .addAll(list) _it works if I use .add(entity) in a loop over the list_.
entity.bodyparts.addAll(bodyparts)
io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entities
at io.objectbox.relation.ToMany.ensureBoxes(ToMany.java:144)
at io.objectbox.relation.ToMany.ensureEntities(ToMany.java:184)
at io.objectbox.relation.ToMany.ensureEntitiesWithTrackingLists(ToMany.java:155)
at io.objectbox.relation.ToMany.trackAdd(ToMany.java:230)
at io.objectbox.relation.ToMany.addAll(ToMany.java:264)
lateinit var bodyparts: ToMany<BodypartEntity>
As mentioned above, the issue seems to be with .addAll specifically, whereas .add does work. The issue happens with detached entities, not using @Id(assignable = true).
Update: This actually seems to be an issue with the BodypartEntity having a defined id (other than 0) even though theyre being inserted for the very first time.
According to the docs you should attach the box before modifying ToMany for entities that use @Id(assignable = true):
entity.id = 123
box.attach(entity) // need to attach box before modifying ToMany
entity.bodyparts.add(bodypart)
This is tracked internally as open-source issue 38.
-ut
Improved error message of DbDetachedException to suggest workaround.
To fix this, ObjectBox would have to know that the entity with an assigned ID is new.
For now consider attaching the box is a required step, if your entity class uses assigned IDs. -ut
@greenrobot-team
@greenrobot
ToMany has too many problems, I am almost going to collapse.
Most helpful comment
@greenrobot-team
@greenrobot
ToMany has too many problems, I am almost going to collapse.