Java greendao and entity inheritance

My task is to perform disk caching on Android OS for my application (this is some kind of messenger). I want to store messages in the database, but I encounter the problem of storing different types of messages (currently there are five message types, each type has its own field, and they all extend the base class)

Green Island documentation said:

Note: at present, it is impossible to use another entity as a superclass (and there is no polymorphic query)

I plan to have an entity almost one-to-one with the base class, in which each subclass can write whatever it needs, except for a column of raw binary or JSON data

My question is:

>In this case, is greendao a good solution? Is there a solution that doesn't have to worry about inheritance - how much it costs in terms of efficiency. > how to "serialize" data into such fields (which method should I override, or where should I place code to complete all necessary operations) > how to provide the correct constructor for greendao to "deserialize" JSON or binary to correct class instances > should I use reflection - or just switch / case to find the correct constructor (only 5 types of constructors can be used) - how much "cost" does reflection reflect in this case?

resolvent:

If you really need to inherit, grendao is not my choice because it does not support inheritance. But I think you can not inherit:

You can design an entity with an identifier column (message type) and a binary or text column (data). Then, you can use the abstract factory to create the required objects from the data according to the message type

If the transformation is complex, put it in a separate class, otherwise keep it as a method in the keep section

Please note that if you do have a lot of messages, this design may slow you down because a separate table will reduce the size of the index. Talking about indexes: if you want to access messages through some properties of data columns in the future, you will be confused because you can't place indexes on them

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>