ActiveAndroid abstract class table
Asked Answered
D

1

17

Does anyone know if it is possible to create a table for an abstract class in ActiveAndroid. For example i could have an abstract class Animal with two concrete classes Dog and Cat. I want to be able to do something like:

List<Animal> animals = new Select().from(Animals.class).execute();

which would result in 'animals' containing all of the saved Cats and Dogs. Or:

Animal animal = new Select().from(Animals.class).where("name = ?", name).executeSingle();

Where 'animal' could be either a Cat or a Dog. Unfortunately when I do this I get an exception because no table is created for the abstract class Animal. Does anyone know how I might go about this using ActiveAndroid?

Desta answered 30/6, 2015 at 11:4 Comment(3)
Can you not use a base class instead of abstraction?Goldfilled
I'm not quite sure I understand why you want to do it this way? The whole purpose of an Abstract class is so that you have to use the child class, such as Cat or Dog. Note how you are creating an Animal animal object, that cannot be done in Java, however your list before hand can be done, though you'll have to cast to the appropriate subclass before working with the object.Irritating
I wouldn't be creating an abstract 'Animal' in the example above. I would want ActiveAndroid to construct either a Cat or Dog depending on the type of the object when it was originally saved.Desta
W
1

Unfortunately ActiveAndroid does not support inheritance of models in that way just yet (https://github.com/pardom/ActiveAndroid/issues/14, https://github.com/pardom/ActiveAndroid/issues/257).

If you wanted to modify ActiveAndroid here's what you could do:

  • Create some kind of annotation that allows you define a model (Animals) as something that isn't persisted (refer to com.activeandroid.annotation.Table).
  • Upon trying to executing queries for that class you could use reflection to determine its child classes and then have it perform that query per child class.
  • Then you would basically take the list of results for each query and combine them into a single list and return that.

To be honest I've never seen inheritance in any Android ORM libraries, and personally I don't think it's a good design pattern for models. You may want to reconsider your reasoning for going down this path.

Washerman answered 8/7, 2015 at 18:41 Comment(4)
What would you recommend over inheritance when persisting my model classes? I want to be able to save 'jobs' which may be different types of 'job'. Would I be better off having a separate table per job type then a 'master' table that stored the type of the job and the foreign key to the correct table for that job type?Desta
@Desta What I would do is create the job model in a very generic way. The columns would be JobType and whatever other properties all jobs have in common. It'd also have an AdditionalProperties column which would be a String that contains a serialized JSONObject of any other job specific properties.Washerman
So in that way I could have a generic 'Job' that would be saved to the db, then each subclassed 'RealJob' could override a "serializeAdditionalProperties()" function that would return the json required to rebuild that object when it was deserialized in future.Desta
@user3265561That could work! As long as you made sure RealJob didn't have it's own table. Though personally I'd probably just keep the one generic Job class and use a bunch of instance methods within it to do different things based on it's typeWasherman

© 2022 - 2024 — McMap. All rights reserved.