I have an base class Peripheral. Classes Sensor and Master are extensions of Peripheral. I need ORMlite to instantiate my Peripheral objects that were previously saved. Obviously any attempt to instantiate Peripheral reflectively will result in a ClassInstantiationException due to its abstractness. How can I have ORMlite load any Peripheral object since Peripheral is abstract?
Here is the sample of what I am doing:
@DatabaseTable(tableName="Peripheral")
abstract class Peripheral {
@DatabaseField(generatedId="true")
int _ID;
@DatabaseField
int mSerial;
}
class Sensor extends Peripheral {
}
class Master extends Peripheral {
}
I think your problem stemmed from the fact that the
@DatabaseTableneeds to be on theSensorandMasterclasses. It won’t hurt it to also be on the base class but it won’t be detected there.This will have one table named
"Peripheral"used for both super-classes. Any fields (such asmSerial) from the base class will be detected and used as fields in both of the super-classes.The only limitation is that ORMLite is unable to differentiate between
SensorandMaster. It right now is missing the feature which allows you to get all rows that are aSensorand get all rows that are aMaster. Also, it cannot generate the schema for them if the super-classes have their own fields marked with@DatabaseField.Edit:
It’s important to reiterate that ORMlite (as of 3/2013) does not support multiple sub-classes in the same table. Subclasses will inherit the database fields from the base class but they have to each be in their own table. Here’s a discussion about this topic on the mailing list.
To accomplish this, you could have 3 tables: 1 for the base class information and 2 for the sub-classes. The sub-class tables would have a foreign key to the associated base class row.