如何延长BaseDaoImp​​l类ORMLite对Android的扩展功能功能、BaseDaoImp、ORMLite、Android

2023-09-05 08:46:34 作者:即便她成遗憾

我不知道是否有一种方法来扩展 BaseDaoImp​​l 在Android类ORMLite的。在我的Andr​​oid项目我使用几种不同的DAO对象来访问不同的业务对象。我的业务对象都存储在不同的表,并都继承形式,它有两个成员的BusinessObject的基类龙ID; 长对象ID; ,其中 ID 是对象的数据库表中的真正的唯一的ID。

 公共抽象类BusinessObject的{

    公共静态最后弦乐ID_COLUMN_NAME =_id;
    公共静态最后弦乐OBJECT_ID_COLUMN_NAME =OBJECT_ID;

    @SerializedName(值=_ ID)
    @DatabaseField(canBeNull =假,COLUMNNAME = ID_COLUMN_NAME,generatedId = TRUE)
    私人诠释身份证;

    @SerializedName(值=ID)
    @DatabaseField(canBeNull =假,COLUMNNAME = OBJECT_ID_COLUMN_NAME,指数=真正的,唯一=真)
    专用长OBJECTID;
}
 

现在我希望能够通过ID和对象ID删除业务对象。删除由id是当然已经可能的,因为BaseDaoImp​​l类。为了能够也删除它们通过OBJECTID我想到了扩展BaseDaoImp​​l类,并添加一个泛型方法deleteByObjectId()方法吧。在这个方法中我会使用DAO的delete()方法,这需要preparedDelete语句删除的对象。

 公共类ExtendedDaoImp​​le< T,ID>扩展BaseDaoImp​​l< T,ID>实现ExtendedDao< T,ID> {

    保护ExtendedDaoImp​​le(类< T>数据类)抛出的SQLException {
        超(数据类);
    }

    公众诠释deleteByObjectId(长OBJECTID)抛出的SQLException {
        DeleteBuilder< T,ID> delBuilder =(DeleteBuilder< T,ID>)deleteBuilder();
        。de​​lBuilder.where()EQ(BusinessObject.OBJECT_ID_COLUMN_NAME,OBJECTID)prepare();
        返回删除(delBuilder prepare());
    }
}
 

我的问题是,我不知道如何创建ExtendedDaoImp​​l类的实例形成OrmLiteSqliteOpenHelper类。通常一个道是通过调用OrmLiteSqliteOpenHelper类getDao()方法并传递道应该用于该类的BusinessObject的创建。例如。

 道<图像,龙> imageDao = getDao(Image.class);
 

那么,有没有办法修改OrmLiteSqliteOpenHelper类这样的方式ExtendedDaoImp​​l对象可以被检索的BaseDaoImp​​l对象,而不是?

解决方案   

我的问题是,我不知道如何创建ExtendedDaoImp​​l类的实例形成OrmLiteSqliteOpenHelper类...

奈斯利措辞问题。该 @DatabaseTable 标注有 daoClass 字段,可用于指定DAO类来构造。

  

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

下面是文档的 DaoManager

  

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

您的类将需要与 ConnectionSource 参数的构造函数。

将溶液没有很好的记载。让我知道,如果你有任何想法我怎么能改进文档。

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?

解决方案

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.