How do you use ORMLite with an abstract class?

后端 未结 2 1670
后悔当初
后悔当初 2021-01-16 06:46

I have an base class Peripheral. Classes Sensor and Master are extensions of Peripheral. I need ORMlite to instantiate my

2条回答
  •  长发绾君心
    2021-01-16 07:22

    Gray is correct, and I forgot about that reflection rule when I asked the question the first time. I have worked around the limitations on both limits of reflection and ORMlite not currently distiguishing between super classes. This is my structure:

    class Peripheral {
        @DatabaseField(generatedId)
        int _ID;
        @databaseField
        int serial;
        // ... Methods ommitted to be succint
    }
    
    class PeripheralWrapper extends Peripheral {
        final Peripheral mBase;
        PeripheralWrapper(Peripheral peripheral) {
            mBase = peripheral;
        }
        // ... Methods are overridden but call the base's methods instead
    }
    
    class Sensor extends PeripheralWrapper {
        Sensor(Peripheral peripheral) {
            super(peripheral);
        }
    }
    
    class Master extends PeripheralWrapper {
        Master(Peripheral peripheral) {
            super(peripheral);
        }
    }
    
    // This is the work around
    
    @DatabaseTable(tableName="Peripheral")
    class BasePeripheral extends Peripheral {
    }
    

    This work around is simple actually. Now I only inflate the basePeripheral and then pass those into the wrappers as needed. All inflation and delegation of wrapping is done in a PeripheralFactory.

提交回复
热议问题