简体   繁体   中英

Hibernate Query not returning results

Long story short my hibernate query is only returning results on its first visit to the DAO from the calling loop.

So far I've tried manually setting the values and that returns its expected result set every time however whenever it turns into a dynamic query it is returning and empty resultset. However that's not what it should be giving me. I've tried multiple instances of querying it with the exact same input for 2 instances through the loop and it will only ever give me results on the first time through.

EDIT: I've narrowed down to my issue is involving the rate_class & utility variables both of which are String's. If these two are 'hardcoded' ie; changing the query to just put in "x" and "y" I get what I want. I've updated the code with my modified version.

Calling Loop:

public void RetrievePricing(){
for(int x=0; x<pricing.size(); x++){
    //grab the pricing object to be priced
    currentlyPricing = pricing.get(x);
    //look back 7 days at max for pricing
    for(int y=0; y<7; y++){             
        //query for pricing matches
        rate_class = currentlyPricing.getRateClass();
        utility = currentUser.getUtility();
        dataList = pricing_dataDao.findPricing(rate_class, 
                        utility, currentUser.getStart_month(), 
                        Integer.valueOf(currentUser.getStart_year()), d1, "12", totalVolume, totalVolume);              
        //if we didn't find one decrement a day
        if(dataList.size() == 0 && x<=0){
            Calendar cal = new GregorianCalendar();
            cal.setTime(d1);
            cal.add(Calendar.DATE, -1);
            d2 = cal.getTime(); 
            d1 = new java.sql.Date(d2.getTime());
        }
        //if we did find something attempt to add it and break out of the inner loop
        else{
            if(currentlyPricing.getName().isEmpty() == false && dataList.isEmpty() == false){
                pricingMap.put(currentlyPricing.getName(), dataList);
                break;
            }

        }
    }
}

}

DAO Function

public List<Pricing_Data> findPricing(String rate_class, String utility, String start_month, int start_year, Date expiration_date, String term, double low_kwh, double high_kwh) {
List<Pricing_Data> tempList = new ArrayList<Pricing_Data>();


    tempList = getHibernateTemplate().find("from Pricing_Data where rate_class=? and utility=? "
            +"and start_month=? and start_year=? and expiration_date=? and term=? and high_kwh>=? and low_kwh<=?"
                , rate_class, utility, start_month, start_year, expiration_date.toString(), term, low_kwh, high_kwh);   
return tempList;

}

Issue was in the line

dataList = pricing_dataDao.findPricing(currentlyPricing.getRateClass().trim(), 
                        currentUser.getUtility(), currentUser.getStart_month(), 
                        Integer.valueOf(currentUser.getStart_year()), d1, "12", totalVolume, totalVolume);  

the Rate Class field is coming from a csv drop down list that is being thrown into a list depending on how many fields were being selected and somewhere along the line I apparently separated them incorrectly so I had to add a .trim() . So for anyone stumbling upon this in the future the Hibernate queries do function correctly and if your String input to your queries is not correctly functioning try a .trim() !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM