gpt4 book ai didi

objectbox - io.objectbox.exception.DbDetachedException : Cannot resolve relation for detached entities

转载 作者:行者123 更新时间:2023-12-05 03:06:27 25 4
gpt4 key购买 nike

在对使用 ObjectBox 数据库的类进行单元测试时出现此错误:

io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entities

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

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

最佳答案

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

我假设异常起源于此处(?):

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 .

关于objectbox - io.objectbox.exception.DbDetachedException : Cannot resolve relation for detached entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com