How to extend the BaseDaoImpl class of ORMLite on

2019-02-04 13:17发布

I'm wondering whether there's a way to extend the BaseDaoImpl class of ORMLite on Android. In my Android project I'm using several different Dao objects to access the different business objects. My business objects are stored in different tables and are all inherited form an BusinessObject base class which has the two members Long id; and Long objectId; where id is the real unique id of the object within the database table.

public abstract class BusinessObject{   

    public static final String ID_COLUMN_NAME = "_id";
    public static final String OBJECT_ID_COLUMN_NAME = "object_id";

    @SerializedName(value="_id")
    @DatabaseField(canBeNull=false, columnName = ID_COLUMN_NAME, generatedId=true)
    private int id;

    @SerializedName(value="id")
    @DatabaseField(canBeNull=false, columnName=OBJECT_ID_COLUMN_NAME, index=true, unique = true)    
    private long objectId;
}

Now I want to be able to delete business objects by id and by objectId. Deleting by id is of course already possible due to the BaseDaoImpl class. To be able to delete them also by objectId I thought about extending the BaseDaoImpl class and adding an generic method deleteByObjectId() method to it. Within the method I would delete the object using the dao's delete() method which takes a PreparedDelete statement.

public class ExtendedDaoImple<T, ID> extends BaseDaoImpl<T, ID> implements ExtendedDao<T, ID> {

    protected ExtendedDaoImple(Class<T> dataClass) throws SQLException {
        super(dataClass);
    }

    public int deleteByObjectId(long objectId) throws SQLException {
        DeleteBuilder<T, ID> delBuilder = (DeleteBuilder<T, ID>) deleteBuilder();       
        delBuilder.where().eq(BusinessObject.OBJECT_ID_COLUMN_NAME, objectId).prepare();
        return delete(delBuilder.prepare());
    }
}

My problem is that I don't know how to create an instance of ExtendedDaoImpl class form the OrmLiteSqliteOpenHelper class. Normally a Dao is created by calling getDao() method of the OrmLiteSqliteOpenHelper class and passing the class of the BusinessObject the Dao should be used for. E.g.

Dao<Image, Long> imageDao = getDao(Image.class);

So is there a way to modify the OrmLiteSqliteOpenHelper class in such a way that ExtendedDaoImpl objects can be retrieved instead of a BaseDaoImpl object?

1条回答
干净又极端
2楼-- · 2019-02-04 14:15

My problem is that I don't know how to create an instance of ExtendedDaoImpl class form the OrmLiteSqliteOpenHelper class...

Nicely worded question. The @DatabaseTable annotation has a field daoClass which can be used to specify the DAO class to construct.

http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/DatabaseTable.html#daoClass()

Here are the docs for the DaoManager.

http://ormlite.com/docs/dao-manager

Your class will need to have a constructor with ConnectionSource and Class arguments.

The solution is not well documented. Let me know if you have any ideas how I can improve the documentation.

查看更多
登录 后发表回答