2012-11-11 53 views
0

我要堅持必須實現這樣的接口部分類:ORMLite和繼承(Android版)

@DatabaseTable(tableName = "conditions") 
abstract public class Condition implements Configurable { 

    @DatabaseField(id = true, foreign = true) 
    private Accion belong_to; 

    @DatabaseField 
    private HashMap<String, String> config = new HashMap<String, String>(); 

    @DatabaseField 
    protected String className = this.getClass().getName(); 

    protected final HashMap<String, String> getConfig() {/* */} 
    protected final void setConfig(HashMap<String, String> config) {/* */} 
    protected void setConfig(String string, String value) {/* */} 
    abstract public String getName(); 
    abstract public int getImageResource(); 
    abstract public String getShortDesc(); 
} 

的事情是,我必須實例的Condition正確的孩子,當我創建它們(如條件本身是抽象的)。

有沒有什麼辦法可以做到這一點,當寫DAO(我記錄className爲此目的)?或者我應該以其他方式做到這一點?

我的Accion對象應該有一個Condition對象附加。 Accion也持續存在,所以獲得條件的途徑也是通過Dao。


更新:我現在用的public interface Configurable extends Serializable { /*...*/ }使Java的可序列化配置對象,我將其存儲在安信永對象

會這樣嗎?

回答

0

由於類反射用於實例化檢索到的記錄,因此存儲的類不應該是抽象類,因爲ClassInstantiationException

使用包裝類發佈here有一項工作,但到目前爲止我的方法一直是將「class」(在我的情況下爲serverType)存儲爲字段,並且在檢索將基類傳遞給構造函數想要的孩子類並複製它的屬性。

用法

 Dao<BaseConnectionInfo, Integer> connections = DatabaseManager 
       .getInstance(this).getDatabaseHelper() 
       .getDao(BaseConnectionInfo.class); 

     BaseConnectionInfo connection = connections.queryForId(1); 

     if (connection.isServerType(BaseConnectionInfo.SERVER_TYPE_MSSQL)) { 
      connection = new MsSqlConnectionInfo(connection); 
     } else if (connection 
       .isServerType(BaseConnectionInfo.SERVER_TYPE_MYSQL)) { 
      connection = new MySqlConnectionInfo(connection); 
     } 

BaseConnectionInfo

@DatabaseTable(tableName = "connections") 
public class BaseConnectionInfo implements SqlDriverInterface { 

    public static final String TAG = "BaseConnection"; 

    public static final String SERVER_TYPE_MYSQL = "MYSQL"; 
    public static final String SERVER_TYPE_MSSQL = "MSSQL"; 
    public static final String SERVER_TYPE_SYBASE = "SYBASE"; 
    public static final String SERVER_TYPE_POSTGRESQL = "POSTGRESQL"; 

    @DatabaseField(generatedId = true) 
    private int id; 

    @DatabaseField 
    protected String serverType; 

    @DatabaseField 
    private String name; 

    ... 

    public boolean isServerType(String serverType) { 
     return this.serverType.equals(serverType); 
    } 

    ... 
} 

MySqlConnectionInfo

public class MySqlConnectionInfo extends BaseConnectionInfo { 

    public MySqlConnectionInfo() { 
     // FOR ORMLITE 
     serverType = SERVER_TYPE_MYSQL; 
    } 

    /** 
    * Constructor used to convert {@link BaseConnectionInfo} to 
    * {@link MySqlConnectionInfo} 
    * 
    * @param baseConnectionInfo 
    */ 
    public MySqlConnectionInfo(BaseConnectionInfo b) { 
     setId(b.getId()); 
     setName(b.getName()); 
     setHost(b.getHost()); 
     setOptionsJson(b.getOptionsJson()); 
    } 
    ... 
} 

MsSqlConnectionInfo

public class MsSqlConnectionInfo extends BaseConnectionInfo { 

    public MsSqlConnectionInfo() { 
     // FOR ORMLITE 
     serverType = SERVER_TYPE_MSSQL; 
    } 

    /** 
    * Constructor used to convert {@link BaseConnectionInfo} to 
    * {@link MsSqlConnectionInfo} 
    * 
    * @param baseConnectionInfo 
    */ 
    public MsSqlConnectionInfo(BaseConnectionInfo b) { 
     setId(b.getId()); 
     setName(b.getName()); 
     setHost(b.getHost()); 
     setOptionsJson(b.getOptionsJson()); 
    } 
    ... 
}