GreenDao and entity inheritance
My task is to create a disk cache for my application on the Android operating system (it’s some kind of messenger). I want to store messages in a database, but I’m having trouble storing different types of messages (currently there are 5 types of messages, each with its own fields, which all extend the base class).
GreenDao documentation:
Note: It is currently impossible to have another entity as the parent (super class) (and no polymorphic queries).
I plan to have entities that are almost 1 to 1 from the base class, except for one column – raw binary or json data, where each subclass can write anything it needs.
My question is:
- Is GreenDao a good solution in this case? Are there any solutions that allow you to worry about inheritance – and how much they cost in terms of efficiency.
- How to “serialize” data into fields like this (what method should I override or where I should put my code, this will do all the necessary things
- How to provide GreenDao with the correct constructor to “deserialize” JSON or binary files with the correct class instance
- Should I use reflection – or just switch/case to find the right constructor (only 5 types of constructors are possible) – how much reflection will “consume” in this case?
Solution
If you really need to inherit greendao is not my choice because it doesn’t support it. But I think you can not inherit :
You can design an entity with a discriminator column (message type) and a binary or text column (data). You can then use the abstract factory to create the required objects from the data based on the message type.
If the conversion is complicated, I’ll put it in a
separate class, otherwise I’ll put it as a method in the keep section.
Note that if you do have a lot of messages, this design may slow you down because separate tables reduce the index size.
Talk about indexes: If you later want to access messages through certain properties of a data column, you’re doomed because you can’t put an index on it.