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

Polymorphic entities #5

Open
greenrobot opened this issue Jan 24, 2017 · 16 comments
Open

Polymorphic entities #5

greenrobot opened this issue Jan 24, 2017 · 16 comments
Labels
enhancement New feature or request low priority

Comments

@greenrobot
Copy link
Member

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.
@greenrobot greenrobot added the enhancement New feature or request label Jan 28, 2017
@Miha-x64
Copy link

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.

@dbacinski
Copy link

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

@YuriDenison
Copy link

  • for Kotlin sealed classes.

@greenrobot
Copy link
Member Author

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

@YuriDenison
Copy link

YuriDenison commented Oct 23, 2017

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'

@Miha-x64
Copy link

[Offtop] Why do you need data modifier here?

@motis10
Copy link

motis10 commented Oct 25, 2017

What about this issue? any updates?

@greenrobot greenrobot modified the milestone: 1.2.0 Oct 25, 2017
@motis10
Copy link

motis10 commented Nov 2, 2017

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

@greenrobot
Copy link
Member Author

greenrobot commented Nov 2, 2017

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.

@motis10
Copy link

motis10 commented Nov 2, 2017

Ok

@greenrobot
Copy link
Member Author

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.

@Armaxis
Copy link

Armaxis commented Jan 16, 2019

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).

@greenrobot-team
Copy link
Member

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

You should thumbs up or discuss #348.

-ut

@xuanswe
Copy link

xuanswe commented Jan 25, 2019

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

@greenrobot-team
Copy link
Member

@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

@xuanswe
Copy link

xuanswe commented Feb 3, 2019

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

This was referenced Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low priority
Projects
None yet
Development

No branches or pull requests

8 participants