原始查询Ormlite(联接,GROUPBY)正确的方法 [英] Raw query to Ormlite (JOINS,GROUPBY) correct way

查看:612
本文介绍了原始查询Ormlite(联接,GROUPBY)正确的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ormlite版本4.46我能当我运行原始查询,但不知怎的,结果为空,当我在ormlite尝试它可以有人请解释我在哪里犯了一个错误,以得到期望的结果。

段:

 查询字符串=
                选择产品。* FROM+ DBConst.TABLE_PRODUCTS
                        +INNER JOIN+ DBConst.TABLE_OFFERS_MAPPING
                        +上的产品。+ DBConst.PROD_ID += OffersMapping。+ DBConst.OFFERS_PRODUCT_ID
                        +WHERE+ DBConst.OFFERS_OFFER_ID +=+ OFFERID +
                        GROUP BY+ DBConst.PROD_PARENT_PRODVAR_ID;        GenericRawResults<产品与GT; rawResults = productDao.queryRaw(查询,productDao.getRawRowMapper());        //产生此查询:SELECT产品* FROM产品INNER JOIN OffersMapping ON Products._id = OffersMapping.product_id WHERE offer_id = 141 GROUP BY variant_id
        清单<产品与GT;产品数= rawResults.getResults();        rawResults.close();

给我想要的结果....
我们ormlite

 道<产品,字符串> ProductDao的= helper.getProductDao();    道< OfferMapping,字符串> offerMappingDao = helper.getOfferMappingDao();    尝试{        QueryBuilder的<产品,字符串> productQb = productDao.queryBuilder();
        QueryBuilder的< OfferMapping,字符串> offerQb = offerMappingDao.queryBuilder();
        //相应排序优惠编号
        。offerQb.where()EQ(DBConst.OFFERS_OFFER_ID,OFFERID);        。productQb.where()EQ(DBConst.PROD_ID,新ColumnArg(DBConst.OFFERS_PRODUCT_ID));        productQb.join(offerQb);        productQb.groupBy(DBConst.PROD_PARENT_PRODVAR_ID);        Constants.showLog(查询,查询+ productQb.query()); //得到空在这里        清单<产品与GT;产品数= productQb.query();
    }赶上(的SQLException E){
        e.printStackTrace();
    }
    返回null;

不知道我在哪里犯了一个错误...


解决方案

  

我使用ormlite版本4.46我能当我运行原始查询,但不知怎的,结果为空,当我在ormlite尝试它可以有人请解释我在哪里犯了一个错误,以得到期望的结果。


对不起,我迟到的反应。我会记录从ORMLite查询生成器生成的查询,然后比较一下你的手生成的查询。您可以登录 productQb的结果。prepareStatementString()进行查询之前。欲了解更多信息,请参阅记录文档

让我知道,如果ORMLite做得不对。

i am using ormlite version 4.46 I am able to get the desired result when i run a raw query but somehow the result is null when i am trying it in ormlite can someone please explain where i am making a mistake.

Snippet:

           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();

Gives me desired result.... Now to 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;

Dont know where i am making a mistake...

解决方案

i am using ormlite version 4.46 I am able to get the desired result when i run a raw query but somehow the result is null when i am trying it in ormlite can someone please explain where i am making a mistake.

Sorry for the late response. I would log the generated query from the ORMLite query-builder and then compare it with your hand generated query. You can log the results of productQb.prepareStatementString() before the query is performed. For more information see the logging documentation.

Let me know if ORMLite is doing something wrong.

这篇关于原始查询Ormlite(联接,GROUPBY)正确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆