question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[1.2.0] [Kotlin] Having TWO ToOne<> relations in one Entity Class. - cannot read back second one.

See original GitHub issue

Issue Basics

  • ObjectBox version (are using the latest version?): 1.2.0
  • Reproducibility: always

Reproducing the bug

Description

If I have an entity with 2 ToOne<> relation fields, one of these fields is not retrieved while reading from boxstore.

@Entity
data class EntityWord(
        @Id var id: Long = 0,
        var wordId: Int? = 0,
        var annotationDetails: ToOne<EntityAnnotation>? = null,
        var lineNo: Int? = 0,
        var wordNo: Int? = 0,
        var audioStart: String? = null,
        var audioEnd: String? = null,
        var mistakes: ToOne<EntityMistakes>? = null
)

EntityAnnotiation is an entity with primitive fields. EntityMistakes is further an Entity with 3 ToMany<> relations.

In this entity, I can retrieve annotationDetails but never mistakes.

I commented annotationDetails just to check, and THEN I can retrieve mistakes.

I’ve used the ObjectBox viewer to verify that the mistakes data does in fact exist in the boxstore.

I cant for the life of me figure out what am i missing in this. Surely, an Entity should be able to support more than ONE ToOne<> relations.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
greenrobot-teamcommented, Apr 17, 2018

Trying to reproduce.

Found cause: relation fields inside a data class constructor break the ObjectBox initialization magic. Kotlin byte-code overwrites the values set by ObjectBox.

Background: the transformer adds the relation field initialization code right before any other statements in the constructor body, right after the super() call. However, Kotlin byte-code for data classes sets all fields again to the values provided through the constructor params.

So

@Entity
data class Example(
    @Id var id: Long = 0,
    val toOne: ToOne<Example>? = null
)

will actually turn into something behaving like:

public Example() {
  this(0L, null);
}

public Example(long id, @Nullable ToOne toOne) {
  this.toOne = new ToOne(this, Example_.toOne); // <-- ObjectBox transformer adds this
  this.id = id;
  this.toOne = toOne;
}

To work around this, specify relation fields as properties instead:

@Entity
data class Example(@Id var id: Long = 0) {
    lateinit var toOne: ToOne<Example>
}

This should also prevent side-effects when ObjectBox tries to build a data class with box data. -ut

1reaction
KishoreBabuINcommented, Nov 6, 2017

Hey Markus Let me isolate the problem into a smaller kotlin project and send you a link.

I’ll need time till tomorrow… I’ve been away from work and travelling.

Cheers!

On 06-Nov-2017, at 6:48 PM, Markus Junginger notifications@github.com wrote:

@barykaed https://github.com/barykaed Any updates? We have cases like you have and it works for us. We need to reproduce it in order to fix it… 😉

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/objectbox/objectbox-java/issues/244#issuecomment-342145709, or mute the thread https://github.com/notifications/unsubscribe-auth/ABQJqigfFgX0HDocjrYxiNboh8Za9P0Fks5szwcbgaJpZM4QOseg.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Kotlin - Error when using one to many entity relationship
Let's say this class - is "read-only" for Room (it is filled with data ... You have another 2 entities - Card and...
Read more >
Micronaut Data - GitHub Pages
Micronaut Data is a database access toolkit that uses Ahead of Time (AoT) ... The above examples return a single instance of an...
Read more >
Spring Data R2DBC - Reference Documentation
With Spring Data, declaring those queries becomes a four-step process: Declare an interface extending Repository or one of its subinterfaces and ...
Read more >
MapStruct 1.5.3.Final Reference Guide
This is the reference documentation of MapStruct, an annotation processor for generating type-safe, performant and dependency-free bean ...
Read more >
JUnit 5 User Guide
Another technical reason for making classes and methods public is to simplify testing on the module path when using the Java Module System....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found