有an issue在github上,但现在objectbox不支持级联删除。 除了手动删除之外,还有其他选择吗?
例如模型:
@Entity
class User(val username: String = "") {
@Id(assignable = true)
var id: Long = 0
@Backlink
lateinit var comments: ToMany<Comment>
}
@Entity
class Comment(var text: String = "", var date: Date = Date()) {
@Id(assignable = true)
var id: Long = 0
@Backlink
lateinit var user: ToOne<User>
}
如果我想删除用户和他的所有评论,我应该这样做:
fun deleteUser(user: User, userBox: Box<User>, commentsBox: Box<Comment>) {
commentsBox.remove(user.comments)
userBox.remove(user)
}
但是如果评论有自己的 child ,而且他们也是——嵌套会非常大。
我想用 BoxStore.subscribe(class: Class<Any>)
, 但它不能给我 User
的实例只删除他的 Comment
如果没有反射(reflection)和其他不好的做法能做到吗?
最佳答案
目前,没有办法手动删除级联。编写您自己的处理嵌套类的删除方法。在单个 transaction 中执行此操作通常是个好主意.
关于java - 在 ObjectBox 中级联删除或对此进行模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49169663/