While unit testing the class that is working with ObjectBox database I got this error:
io.objectbox.exception.DbDetachedException: Cannot resolve relation for detached entities
I have implemented all the necessary data into my Entity class for unit testing on macOs. So the entity relation with a to-one and a to-many relation pointing to itself.
@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)
}
}
Unit test is very simple:
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());
}
}
And actual class that I am testing is normal mapper:
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) }
}
}
I have some other tests that are testing the parent
and subCategories
in more detail, but I got the same error in all of the tests.
Before it was working fine, but that something went wrong.
Do you have a full stack trace? Otherwise it is hard to pinpoint the issue.
I am assuming that the exception originates somewhere here (?):
from.subCategories?.let {
it.forEach{ toModel.subCategories?.add( transform(it)) }
}
And that CategoryModel is also an entity with @Id(assignable=true)
?
toModel.id = from.id
In that case make sure to call box.attach(toModel)
before modifying the ToMany
:
CategoryModel toModel = CategoryModel()
toModel.id = from.id
box.attach(toModel) // need to attach box first
toModel.subCategories.add(...)
Source: See the Updating ToMany section in the ObjectBox Relations documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With