objectbox / objectbox/objectbox-java
Can't save same Kotlin data class multiple times with ToMany
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 4.6k
- Forks
- 311
- PR merge metrics
- No merged PRs in 30d
Description
Issue Basics
- ObjectBox version (are using the latest version?): 2.0.0
- Reproducibility: always
Reproducing the bug
Description
I'm using ObjectBox to save data. When I save the same data with ToMany, it only save the first one.
I write a test, the size of result.details is 1.
Can I save multiple same data with ToMany? How can I fix it?
Thanks for answer.
ObjectBox(version:2.0.0)
Android(target sdk version:27)
Kotlin(version:1.2.60)
Code
@Test
fun orderDetailTest() {
val orderBox: Box<Order> = store.boxFor()
// 儲存兩個內容相同的資料
val order = Order().apply {
this.details.add(OrderDetail(productSpecName = "ABC"))
this.details.add(OrderDetail(productSpecName = "ABC"))
}
val orderId = orderBox.put(order)
val result = orderBox.get(orderId)
assert(result.details.size == 2)
}
Entities
@Entity
data class Order(@Id var id: Long = 0,
val createTime: Long = 0,
val total: Int = 0) {
@Backlink(to = "order")
lateinit var details: ToMany<OrderDetail>
}
@Entity
data class OrderDetail(@Id var id: Long = 0,
val productName: String = "",
val productSpecName: String = "",
val productSpecPrice: Int = 0,
val quantity: Int = 0,
val subtotal: Int = 0) {
lateinit var order: ToOne<Order>
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the orderDetailTest case with the shown Order and OrderDetail entities, then inspect how the ToMany backlink and ToOne relation are persisted. Done means saving two identical OrderDetail instances results in result.details.size == 2, or the expected limitation is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- database, mobile
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100