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

时间:2018-03-19 08:27:12

标签: objectbox

在对使用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,但我在所有测试中都遇到了同样的错误。 在它工作正常之前,但是出了点问题。

1 个答案:

答案 0 :(得分:1)

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

我假设异常来自某处(?):

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

该CategoryModel也是@Id(assignable=true)的实体?​​

toModel.id = from.id

在这种情况下,请务必在修改box.attach(toModel)

之前致电ToMany
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