原始查询Ormlite(JOINS,GROUPBY)的正确方法

时间:2013-10-09 12:46:12

标签: android sqlite ormlite

我正在使用ormlite版本4.46我能够在运行原始查询时获得所需的结果但是当我在ormlite中尝试它时某种程度上结果为null可以有人请解释我在哪里犯了错误。

段:

           String query=
                "SELECT Products.* FROM "+DBConst.TABLE_PRODUCTS
                        +" INNER JOIN "+DBConst.TABLE_OFFERS_MAPPING
                        +" ON Products."+DBConst.PROD_ID+" = OffersMapping."+DBConst.OFFERS_PRODUCT_ID
                        +" WHERE "+DBConst.OFFERS_OFFER_ID+ " = "+offerId+
                        " GROUP BY "+DBConst.PROD_PARENT_PRODVAR_ID;

        GenericRawResults<Product> rawResults = productDao.queryRaw(query, productDao.getRawRowMapper());

        //produces this query:SELECT Products.* FROM Products INNER JOIN OffersMapping ON Products._id = OffersMapping.product_id WHERE offer_id = 141 GROUP BY variant_id
        List<Product> prodList = rawResults.getResults();

        rawResults.close();

给我理想的结果...... 现在到ormlite

    Dao<Product, String> productDao = helper.getProductDao();

    Dao<OfferMapping, String> offerMappingDao = helper.getOfferMappingDao();

    try {

        QueryBuilder<Product, String> productQb = productDao.queryBuilder();
        QueryBuilder<OfferMapping, String> offerQb = offerMappingDao.queryBuilder();
        //to sort the offer id accordingly
        offerQb.where().eq(DBConst.OFFERS_OFFER_ID, offerId);

        productQb.where().eq(DBConst.PROD_ID, new ColumnArg(DBConst.OFFERS_PRODUCT_ID));

        productQb.join(offerQb);

        productQb.groupBy(DBConst.PROD_PARENT_PRODVAR_ID); 

        Constants.showLog("Query", "Query is "+productQb.query());//gets null here

        List<Product> prodList = productQb.query();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;

不知道我在哪里弄错了......

1 个答案:

答案 0 :(得分:1)

  

我正在使用ormlite版本4.46我能够在运行原始查询时获得所需的结果但是当我在ormlite中尝试它时某种程度上结果为null可以有人请解释我在哪里犯了错误。

对于迟到的回复感到抱歉。我将从ORMLite查询构建器中记录生成的查询,然后将其与手动生成的查询进行比较。您可以在执行查询之前记录productQb.prepareStatementString()的结果。有关详细信息,请参阅logging documentation

如果ORMLite做错了,请告诉我。