Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query link with ToOne condition #537

Closed
Nauce opened this issue Aug 23, 2018 · 6 comments

Comments

@Nauce
Copy link

commented Aug 23, 2018

Issue Basics

  • ObjectBox version: 2.1.0
  • Reproducibility: always

Reproducing the bug

Description

Querying with related ToOne entity and ToMany entities returns no results:
Persons who work at Company "1" and have an address on "Sesame Street"

Code

// get all persons who work at Company 1
val builder = personBox.query()
    .equal(Person_.companyId, 1)

// ...which have an address on "Sesame Street"
builder.link(Person_.addresses).equal(Address_.street, "Sesame Street")
val result = builder.build().find()

Result is empty

// get all Person objects
val builder = personBox.query()
    // .equal(Person_.companyId, 1) <- without this condition, there are results

// ...which have an address on "Sesame Street"
builder.link(Person_.addresses).equal(Address_.street, "Sesame Street")
val result = builder.build().find()

Result with Elmo Person

Initialization

...
companyBox.put(Company().apply {
    id = 1
    name = "Google"
})
personBox.put(Person().apply {
    id = 1
    name = "Elmo"
    company.targetId = 1
})
addressBox.put(Address().apply {
    id = 1
    street = "Sesame Street"
 })
personBox.get(1).addresses.applyChangesToDb {
    addAll(addressBox.all)
}

Entities

@Entity
class Person {
    @Id(assignable = true)
    var id: Long = 0
    var name: String? = null

    lateinit var addresses: ToMany<Address>
    lateinit var company: ToOne<Company>
}
@Entity
class Address {
    @Id(assignable = true)
    var id: Long = 0
    var street: String? = null
    var zip: String? = null

    @Backlink(to = "addresses")
    lateinit var persons: ToMany<Person>
}
@Entity
class Company {

    @Id(assignable = true)
    var id: Long = 0
    var name: String? = null

    @Backlink(to = "company")
    lateinit var persons: ToMany<Person>

}
@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

commented Aug 27, 2018

Verified. Thanks for reporting this!

Will add an internal test for this which will hopefully lead to a fix.
-ut

@greenrobot-team greenrobot-team added the bug label Aug 27, 2018
@greenrobot-team greenrobot-team added this to the 2.2 milestone Aug 27, 2018
@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

commented Aug 27, 2018

Workaround:

val result = personBox.query().apply {
    link(Person_.company).equal(Company_.id, 1)
    link(Person_.addresses).equal(Address_.street, "Sesame Street")
}.build().find()

However, this requires explicitly adding Person.companyId, like:
var companyId: Long = 0

Update Some notes on dropping the requirement of explicitly adding the target ID property:

To do
link(Person_.company).equal(Company_.id, 1)
the nativeLink() call requires a propertyId.

However, if the target ID property for the ToOne is not explicitly added RelationInfo Person_.company targetIdProperty is null. This is to differentiate from a ToOne with an explicit target ID property.

So no propertyId is passed to nativeLink() and it will fail to find the relation.

(Note: the RelationInfo of the @Backlink ToMany Company_.persons does have the target ID property of the linked ToOne set, this ensures ToMany works.)
-ut

@greenrobot-team greenrobot-team removed this from the 2.2 milestone Aug 27, 2018
@Nauce

This comment has been minimized.

Copy link
Author

commented Aug 27, 2018

Thank you! Meanwhile, I am going to take that option.

@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

commented Aug 28, 2018

Made changes to remove the need to explicitly define the target ID property. Update: available in 2.2.0.

This leaves the issue that adding a condition on the target ID property does not work in combination with link(). Passed on internally to the development team.
-ut

greenrobot pushed a commit that referenced this issue Sep 28, 2018
This will replace the null check on RelationInfo.targetIdProperty to
determine if a property is virtual.

#537
@greenrobot-team greenrobot-team added this to the 2.4.0 milestone Jul 22, 2019
@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

commented Sep 23, 2019

A fix was issued internally and will be available with the next update. Thanks again for reporting.

@greenrobot

This comment has been minimized.

Copy link
Member

commented Oct 3, 2019

This is also part of the just released version '2.4.0-RC' - would be great if you could give it a try and report back. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.