如何删除ORM-Lite中包含多个对象的对象

时间:2014-12-03 17:17:29

标签: android orm ormlite

我正在为Android应用程序使用ORM-lite,Model类如下,

@DatabaseTable(tableName = "pageimage")
public class PageImage
{

@DatabaseField(generatedId = true)
private int pageimageId;
@DatabaseField(foreign = true, foreignAutoRefresh = true, uniqueCombo=true)
private Page page;
@DatabaseField(foreign = true, foreignAutoRefresh = true, uniqueCombo=true)
private Image image;
@DatabaseField
private int order;


//implementation

}

PageImage对象是使用引用页面和图像表的Page和Image对象的组合创建的。我已经搜索了很多,但仍然无法找到一个删除PageImage对象,其中Page id ="某些值"和图像ID ="某些值" 。

欣赏任何想法。

1 个答案:

答案 0 :(得分:1)

您可以使用DeleteBuilder来执行此操作。

    private void deletePageItem(Page page, Image image) {
    try {
        Dao<PageItem, ?> dao = mOpenHelper.getDao(PageItem.class);
        DeleteBuilder<PageItem, ?> deleteBuilder = dao.deleteBuilder();
        deleteBuilder.where().eq(PageItem.COLUMN_IMAGE, image).and().eq(PageItem.COLUMN_PAGE, page);
        deleteBuilder.delete();

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

其中mOpenHelper是从OrmLiteSqliteOpenHelper

扩展的实体

PageItem.java

@DatabaseTable(tableName = "pageimage")
public class PageItem {

    public static final String COLUMN_PAGE = "page";
    public static final String COLUMN_IMAGE = "image";

    @DatabaseField(generatedId = true)
    private int pageimageId;
    @DatabaseField(columnName = COLUMN_PAGE, foreign = true, foreignAutoRefresh = true, uniqueCombo = true)
    private Page page;
    @DatabaseField(columnName = COLUMN_IMAGE, foreign = true, foreignAutoRefresh = true, uniqueCombo = true)
    private Image image;
    @DatabaseField
    private int order;


}