objectbox / objectbox/objectbox-java

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

Open
#528 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.