Objectbox-java: Polymorphic entities

Created on 24 Jan 2017  路  16Comments  路  Source: objectbox/objectbox-java

Allow subclassing of entities.

Affects:

  • Box: should baseBox.get(subclassId) return a subclass entity? Probably so.
  • Queries: Querying for the base class should return all sub classes.
  • Relations: if a relation references a base class, actual objects may also be sub classes.
feature low priority

Most helpful comment

Below is an example that doesn't work:

sealed class Card(...)

@Entity
data class CardA(...) : Card() {
    lateinit var session: ToOne<Session> // because of current limitation, relationships must be declared here
}

@Entity
data class CardB(...) : Card() {
    lateinit var session: ToOne<Session> // because of current limitation, relationships must be declared here
}

@Entity
data class Session(...) {
    lateinit var cards: ToMany<Card> // this line demonstrates a common use case but sadly does not work (yet?)
}

It is nice if these things are supported

Queries: Querying for the base class should return all sub classes.
Relations: if a relation references a base class, actual objects may also be sub classes.

Workaround in my project

  • don't use entity inheritance
  • put all properties in single entity Card
  • control which properties are available for different Card types at logic layer

All 16 comments

Creating schema for such entities looks quite complicated. How database should handle object of previously unknown subclass with many new fields?
Maybe Kotlin sealed classes may help: all allowed subclasses are known at compile-time.

I would put it at the end of backlog, we can live without polimorfic types.

  • for Kotlin sealed classes.

@YuriDenison Can you give an example why one would used sealed classes as entities?

Simple example

sealed class Destination {
  abstract val createdAt: Long

  data class City(...) : Destination()
  data class Hotel(...): Destination()
}

It would be nice to be able to create Destination box and make queries like 'get all destinations ordered by createdAt'

[Offtop] Why do you need data modifier here?

What about this issue? any updates?

So important to the correct usage of the library my friends. You have to fix it :)

This will introduce complexity quite a bit. Other features have a better value/cost ratio. Thus we'll put that feature a bit further down in the backlog so you know what to expect.

Ok

Update: ObjectBox 1.4 introduces (non-polymorphic) entity inheritance. You can check it out it now using version 1.4.0-RC and also have a look at the docs.

I have a reversed problem I'm trying to solve: I want to store subclass entities (which have 20+ fields) and be able to retrieve both subclass entities (with all the fields) and baseclass entities (only 5 fields). Is it possible to achieve using current Inheritance implementation?

The reason for this is that we have a lot of data, and each entry takes 1-2Kb of memory. On devices with limited amount of memory, we want to have a slimmed-down version of the object with only essential fields (5 fields).

@Armaxis Sorry, the current inheritance support persists and loads all properties.

You should thumbs up or discuss #348.

-ut

really sad because this issue is low priority. I think inheritance is a very common design pattern and should have a higher priority.

@nguyenxndaidev Entity inheritance (with limitations) is available since ObjectBox 1.4.
https://docs.objectbox.io/advanced/entity-inheritance

Please share what you need that is not supported right now.
-ut

Below is an example that doesn't work:

sealed class Card(...)

@Entity
data class CardA(...) : Card() {
    lateinit var session: ToOne<Session> // because of current limitation, relationships must be declared here
}

@Entity
data class CardB(...) : Card() {
    lateinit var session: ToOne<Session> // because of current limitation, relationships must be declared here
}

@Entity
data class Session(...) {
    lateinit var cards: ToMany<Card> // this line demonstrates a common use case but sadly does not work (yet?)
}

It is nice if these things are supported

Queries: Querying for the base class should return all sub classes.
Relations: if a relation references a base class, actual objects may also be sub classes.

Workaround in my project

  • don't use entity inheritance
  • put all properties in single entity Card
  • control which properties are available for different Card types at logic layer
Was this page helpful?
0 / 5 - 0 ratings

Related issues

greenrobot picture greenrobot  路  47Comments

livotov picture livotov  路  17Comments

DJafari picture DJafari  路  21Comments

Gaket picture Gaket  路  79Comments

greenrobot picture greenrobot  路  53Comments