objectbox / objectbox/objectbox-java

Can't save same Kotlin data class multiple times with ToMany

未關閉
#528 7 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

enhancement
主要語言
Java
星號
4.6k
分支
311
PR 合併指標
30 天內沒有已合併 PR

描述

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>
}

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先使用所示的 Order 和 OrderDetail 實體重現 orderDetailTest 案例,然後檢查 ToMany 反向連結和 ToOne 關係是如何持久化的。當儲存兩個相同的 OrderDetail 執行個體後得到 result.details.size == 2,或記錄預期的限制時,即視為完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
android, kotlin
領域
database, mobile
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。