Objectbox-java: Detached entities 'ToMany.addAll' throws error

Created on 28 Mar 2018  路  4Comments  路  Source: objectbox/objectbox-java

Issue Basics

  • ObjectBox version 1.4.4
  • Reproducibility: Always

Reproducing the bug

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_.

Code

entity.bodyparts.addAll(bodyparts)

Logs & stackstraces

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)

Entities

lateinit var bodyparts: ToMany<BodypartEntity>

Misc

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).

bug

Most helpful comment

@greenrobot-team
@greenrobot
ToMany has too many problems, I am almost going to collapse.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dj-4war picture dj-4war  路  5Comments

dgandhi17 picture dgandhi17  路  4Comments

thanhhai08sk picture thanhhai08sk  路  3Comments

ScottPierce picture ScottPierce  路  3Comments

cincospenguinos picture cincospenguinos  路  5Comments