Java Hibernate查询不返回结果
长话短说,我的hibernate查询只在第一次从调用循环访问DAO时返回结果 到目前为止,我已经尝试手动设置这些值,并且每次都返回预期的结果集,但是每当它变成动态查询时,它都返回空的结果集。然而,这不是它应该给我的。我已经尝试了使用完全相同的输入在循环中查询它的多个实例,共有2个实例,它只会在第一次查询时给出结果 编辑: 我已经缩小到我的问题涉及到rate_类和效用变量,这两个变量都是字符串。如果这两个是“硬编码”的,即;将查询更改为只输入“x”和“y”,我就得到了我想要的。我已经用修改过的版本更新了代码 调用循环:Java Hibernate查询不返回结果,java,sql,spring,hibernate,Java,Sql,Spring,Hibernate,长话短说,我的hibernate查询只在第一次从调用循环访问DAO时返回结果 到目前为止,我已经尝试手动设置这些值,并且每次都返回预期的结果集,但是每当它变成动态查询时,它都返回空的结果集。然而,这不是它应该给我的。我已经尝试了使用完全相同的输入在循环中查询它的多个实例,共有2个实例,它只会在第一次查询时给出结果 编辑: 我已经缩小到我的问题涉及到rate_类和效用变量,这两个变量都是字符串。如果这两个是“硬编码”的,即;将查询更改为只输入“x”和“y”,我就得到了我想要的。我已经用修改过的版本
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;
}
}
}
}
public void RetrievePricing(){
对于(int x=0;x而言,问题已在一线
dataList = pricing_dataDao.findPricing(currentlyPricing.getRateClass().trim(),
currentUser.getUtility(), currentUser.getStart_month(),
Integer.valueOf(currentUser.getStart_year()), d1, "12", totalVolume, totalVolume);
Rate Class
字段来自一个csv下拉列表,该列表将根据选择的字段数量被放入一个列表中,而在这行的某个地方,我显然没有正确地将它们分开,因此我不得不添加一个.trim()
。因此,对于将来遇到此问题的任何人,Hibernate
查询确实可以正常运行,如果查询的字符串
输入功能不正常,请尝试.trim()
!我想在DAO方法周围加上一个事务,即在打开会话之后和关闭会话之前。@Zeus您指的是transaction x=session.BeginTransaction()那么x.commit?是的,在那之前,你正在使用hibernate模板,我想知道为什么在你已经使用hql来做同样的事情时,为什么要将它与现有代码混合使用?@Zeus如果你指的是创建查询的两种不同方法,我这样做只是为了看看问题是否在查询本身。我还做了一些额外的测试,发现手动执行会话/事务,而不是让spring/hibernate处理它们,我得到了相同的结果。
dataList = pricing_dataDao.findPricing(currentlyPricing.getRateClass().trim(),
currentUser.getUtility(), currentUser.getStart_month(),
Integer.valueOf(currentUser.getStart_year()), d1, "12", totalVolume, totalVolume);