io.objectbox.exception.DbDetachedException:无法解析分离实体的关系
io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entities
在对与 ObjectBox 数据库一起使用的 class 进行单元测试时,我收到此错误:
io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entities
我已经将所有必要的数据实现到我的实体 class 中,以便在 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 是法线映射器:
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) }
}
}
我还有一些其他测试正在更详细地测试 parent
和 subCategories
,但我在所有测试中都遇到了相同的错误。
之前它工作正常,但出了点问题。
你有完整的堆栈跟踪吗?否则很难查明问题。
我假设异常起源于此处 (?):
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 数据库一起使用的 class 进行单元测试时,我收到此错误:
io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entities
我已经将所有必要的数据实现到我的实体 class 中,以便在 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 是法线映射器:
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) }
}
}
我还有一些其他测试正在更详细地测试 parent
和 subCategories
,但我在所有测试中都遇到了相同的错误。
之前它工作正常,但出了点问题。
你有完整的堆栈跟踪吗?否则很难查明问题。
我假设异常起源于此处 (?):
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。