【问题标题】:Exception : io.objectbox.exception.DbDetachedException Even though entity is attached异常:io.objectbox.exception.DbDetachedException 即使附加了实体
【发布时间】:2019-05-06 22:14:36
【问题描述】:

将 Jackson 与 Retrofit 结合使用,我想在反序列化期间将朋友列表设置为 friendToMany。正如我已经完成的文档一样,我们必须在设置 assignable=true 时手动将该实体分配给 boxstore。所以,我正在这样做(如代码所示)。此方法仅适用于此代码所属的第一项。它不适用于元素 2 或更多元素。

@Id(assignable = true)
@JsonProperty("_id")
public long id;

@Transient
private List<Friend> friends = null;

@JsonIgnore
@Backlink(to = "demoResponseToOne")
ToMany<Friend> friendToMany;

@JsonProperty("friends")
public void setFriends(
        List<Friend> friends)
    {
    this.friends = friends;
    for (Friend friend : friends)
        {
        MyApplication.getBoxStore().boxFor(Friend.class).attach(friend);
        friendToMany.add(friend);
        }
    }

抛出的异常是:io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entities, call box.attach(entity) beforehand.add(friend)。我的意思是当这个 Root 元素是列表的第一项时,这有效。

【问题讨论】:

    标签: java android database jackson2 objectbox


    【解决方案1】:

    您还需要将Box&lt;Friend&gt; 附加到this,它拥有要修改的ToMany

    MyApplication.getBoxStore().boxFor(Friend.class).attach(this);
    

    背景:如果您使用@Id(assignable = true),您需要注意一些ObjectBox 通常会为您做的事情。这包括在修改任何ToMany 之前附加Box

    来源: https://docs.objectbox.io/relations#updating-tomany https://docs.objectbox.io/advanced/object-ids#self-assigned-object-ids

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 2017-12-21
      相关资源
      最近更新 更多