【问题标题】:io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entitiesio.objectbox.exception.DbDetachedException:无法解析分离实体的关系
【发布时间】:2018-03-19 08:27:11
【问题描述】:

在对使用 ObjectBox 数据库的类进行单元测试时,我收到了这个错误:

io.objectbox.exception.DbDetachedException:无法解析分离实体的关系

我已经在我的 Entity 类中实现了所有必要的数据,以便在 macOS 上进行单元测试。因此,实体关系具有指向自身的一对一和一对多关系。

@Entity
data class Category(
    @Id var id: Long? = 0,
    var title: String?,
    @JvmField var parent: ToOne<Category>? = null,
    @JvmField @Backlink(to = "parent") var subCategories: ToMany<Category>? = null){

    constructor(): this(id = 0, title =  "", parent = null, subCategories = null)
    constructor(title: String): this(id = 0, title = title, parent = null, subCategories = null)

    // normally transformer would add field, but need to manually for local unit tests
    @JvmField @Transient var __boxStore: BoxStore? = null

    init{
        if(parent == null) parent = ToOne(this, Category_.parent)
        if(subCategories == null) subCategories = ToMany(this, Category_.subCategories)
    }
}

单元测试很简单:

public class CategoryModelDataMapperTest {

    private CategoryModelDataMapper categoryModelDataMapper = new CategoryModelDataMapper();

    @Before
    public void setUp() throws Exception {
        categoryModelDataMapper = new CategoryModelDataMapper();
    }

    @Test
    public void testShouldTransformDomainModelWithoutParentToEntityCorrectly() throws Exception{
        final Category category = new Category(10L, "Bar", null, null);
        final CategoryModel categoryModel = categoryModelDataMapper.transform(category);

        assertEquals((Long) 10L, categoryModel.getId());
        assertEquals("Bar", categoryModel.getTitle());
        assertNull(categoryModel.getParent());
        assertNotNull(categoryModel.getSubCategories());
        assertTrue(categoryModel.getSubCategories().isEmpty());
    }
}

我正在测试的实际类是法线映射器:

class CategoryModelDataMapper: BaseMapper<Category, CategoryModel>{

    override fun transform(from: Category): CategoryModel {
        val toModel = CategoryModel()
        toModel.id = from.id
        toModel.title = from.title

        toModel.parent = from.parent?.target?.let { transform(it) }

        from.subCategories?.let {
            it.forEach{ toModel.subCategories?.add( transform(it)) }
        }

        return toModel
    }

    override fun transform(fromList: MutableList<Category>): MutableList<CategoryModel> {
        val toList = ArrayList<CategoryModel>(fromList.size)
        return fromList.mapTo(toList) { transform(it) }
    }
}

我还有一些其他测试正在更详细地测试parentsubCategories,但我在所有测试中都遇到了同样的错误。 之前它工作正常,但出了点问题。

【问题讨论】:

    标签: objectbox


    【解决方案1】:

    你有完整的堆栈跟踪吗?否则很难查明问题。

    我假设异常源自这里的某个地方(?):

    from.subCategories?.let {
        it.forEach{ toModel.subCategories?.add( transform(it)) }
    }
    

    而且那个 CategoryModel 也是@Id(assignable=true)的实体?

    toModel.id = from.id
    

    在这种情况下,请务必在修改 ToMany 之前调用 box.attach(toModel)

    CategoryModel toModel = CategoryModel()
    toModel.id = from.id
    box.attach(toModel) // need to attach box first
    toModel.subCategories.add(...)
    

    来源:见Updating ToMany section in the ObjectBox Relations documentation

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 1970-01-01
      • 2022-11-23
      • 2021-09-10
      • 2018-12-15
      • 2019-05-06
      • 2015-01-30
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多