有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将Hibernate查询转换为3个条件的子查询

这种方法完全符合我的要求。但是我只想用DB查询一次

对于在UI中查询和显示列表数据,我有两个条件需要与DB进行检查。每次我访问数据库并检查列表大小和执行操作时

对于同一个表的一个数据列表,我查询了3次。我认为这不是一种有效的方式。并为其编写子查询。但却无法成功

那么,有没有什么有效的方法来写这篇文章并优化我的方法

查询1"FROM Produce where produceId not in (:produceIdList) and itemName in (:itemNameList)" + " and farmerUuid not in(" + merchantUuid + ") and lastDateForBid>=CURDATE() order by lastDateForBid Asc";

Condition: If(result>=10) return data else check for query2

查询2"FROM Produce where produceId not in (:produceIdList) and farmerUuid not in(" + merchantUuid + ")" + "and itemName in (:itemNameList) and categoryId in (:categoryList) and lastDateForBid>=CURDATE() order by lastDateForBid Asc";

Condition: If(result>=10) return data else check for query3

查询3

"FROM Produce where produceId not in (:produceIdList) and farmerUuid not in(" + merchantUuid + ") and lastDateForBid>=CURDATE() order by lastDateForBid Asc ";

这是我的方法

    mId = merchantUuid;
    Long produceId = null;
    String itemName = null;
    Long categoryId = null;
    List<Long> categoryList = new ArrayList<Long>();
    List<Long> produceIdList = new ArrayList<Long>();
    List<Produce> produceList = new ArrayList<Produce>();
    List<String> itemNameList = new ArrayList<String>();
    List<Bidding> biddingList = getBiddingForMerchant();
    int count = biddingList.size();
    if (count > 0) {
        for (int i = 0; i < count; i++) {
            produceId = biddingList.get(i).getProduce().getProduceId();
            if(!produceIdList.contains(produceId)){
            produceIdList.add(produceId);
            }
            itemName = biddingList.get(i).getProduce().getItemName();
            if (!itemNameList.contains(itemName)) {
                itemNameList.add(itemName);
            }
            categoryId = biddingList.get(i).getProduce().getCategory().getCategoryId();
            if (!categoryList.contains(categoryId)) {
                categoryList.add(categoryId);
            }
        }
        String ProduceQuery1 = "FROM Produce where produceId not in (:produceIdList)"
                + " and itemName in (:itemNameList)" + " and farmerUuid not in(" + merchantUuid + ")"
                + " and lastDateForBid>=CURDATE() order by lastDateForBid Asc";
        Query q1 = sessionFactory.getCurrentSession().createQuery(ProduceQuery1);
        q1.setParameterList("itemNameList", itemNameList);
        q1.setParameterList("produceIdList", produceIdList);
        //q1.setMaxResults(10);
        @SuppressWarnings("unchecked")
        List<Produce> produceList1 = q1.list();
        produceList.addAll(produceList1);
        if (produceList.size() ==10) {
            return produceList;
        }

        else if (produceList.size() < 10) {
            String produceQuery2 = "FROM Produce where produceId not in (:produceIdList)"
                    + " and farmerUuid not in(" + merchantUuid + ")" + "and itemName in (:itemNameList)"
                    + " and categoryId in (:categoryList) "
                    + " and lastDateForBid>=CURDATE() order by lastDateForBid Asc";
            Query q2 = sessionFactory.getCurrentSession().createQuery(produceQuery2);
            System.out.println("produceQuery::" + produceQuery2);
            q2.setParameterList("produceIdList", produceIdList);
            q2.setParameterList("itemNameList", itemNameList);
            q2.setParameterList("categoryList", categoryList);
            //q2.setMaxResults(10);
            @SuppressWarnings("unchecked")
            List<Produce> produceList2 = q2.list();
            produceList.clear();
            produceList.addAll(produceList2);

            if (produceList.size() > 9) {
               List<Produce> produceListNew = produceList.subList(0,10);
               return produceListNew;
            }

            else {
                String produceQuery3 = "FROM Produce where produceId not in (:produceIdList)"
                        + " and farmerUuid not in(" + merchantUuid + ")"
                        + " and lastDateForBid>=CURDATE() order by lastDateForBid Asc ";
                Query q3 = sessionFactory.getCurrentSession().createQuery(produceQuery3);
                q3.setParameterList("produceIdList", produceIdList);
                //q3.setMaxResults(10);
                @SuppressWarnings("unchecked")
                List<Produce> produceList3 = q3.list();
                produceList.clear();
                produceList.addAll(produceList3);

                if(produceList.size()>10){
                produceList = produceList.subList(0,10);
                }
                return produceList;
            }
        }
    }

谢谢你


共 (0) 个答案