java - hibernate3中索引越界异常

标签 java spring hibernate exception

在我的项目中,我以周的方式存储和检索时间表。 我有一张这样的 table

Id projectId activityId     Date       Spenttime
1     1       1          2014-11-10     8  
2     1       1          2014-11-11     8  
3     1       1          2014-11-12     8  
4     1       1          2014-11-13     8  
5     1       1          2014-11-14     8  
6     1       1          2014-11-15     8  
7     1       1          2014-11-16     8  
8     1       2          2014-11-10     8  
9     1       2          2014-11-11     8  
10    1       2          2014-11-12     8  
11    1       2          2014-11-13     8  
12    1       2          2014-11-14     8  
13    1       2          2014-11-15     8  
14    1       2          2014-11-16     8
15    2       1          2014-11-15     8
16    2       1          2014-11-16     8 

我想要上表的结果,如下所示

projectId activityId 2014-11-10  2014-11-11 2014-11-12 2014-11-13 2014-11-14 2014-11-15 2014-11-16 
 1           1          8             8         8          8         8          8          8
 1           2          8             8         8          8         8          8          8
 2           1          0             0         0          0         0          8          8

上表的我的 hibernate 代码

List<Timesheet> timesheetList=sessionfactory.getCurrentSession.createCriteria(Timesheet.class)
                              .add(Restrctions.between("date",formatter.parse("2014-11-09"),formatter.parse("2014-11-16"))
                              .list();    

检索逻辑:

List<DisplayTable> dispaly=new ArrayList<DisplayTable>();
for(int i=0;i<timesheetList.size();i+=7)
{
DisplayTable disp=new DisplayTable();
disp.setProjectId(timesheetList.get(i).getProjectId());
disp.setActivityId(timesheetList.get(i).getActivityId());
disp.setSpentTimeDate1(timesheetList.get(i).getSpentTime());
disp.setSpentTimeDate2(timesheetList.get(i+1).getSpentTime());
disp.setSpentTimeDate3(timesheetList.get(i+2).getSpentTime());
disp.setSpentTimeDate4(timesheetList.get(i+3).getSpentTime());
disp.setSpentTimeDate5(timesheetList.get(i+4).getSpentTime());
disp.setSpentTimeDate6(timesheetList.get(i+5).getSpentTime());
disp.setSpentTimeDate7(timesheetList.get(i+6).getSpentTime());
}

上面的逻辑在前两次迭代中工作正常。之后它抛出索引越界异常。 我知道抛出异常是因为项目 2 仅包含 2 行。

他们有什么方法可以在 hibernate3 中达到预期的结果吗? 任何帮助将不胜感激!!!!

最佳答案

将条件更改为

 i < timesheetList.size() - 6

因为你不想比尺寸+7

关于java - hibernate3中索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26978332/

相关文章:

java - SOA 客户端握手错误

java - 在java中构建kibana查询以从elasticsearch中获取记录

java - Spring mvc - 发出发布请求并在出现错误时保持在同一页面上

java - xml 文件中的 Spring 表达式语言扩展

java - JPA/Hibernate 单向 OneToMany 为 'mappedBy'

java - 未创建审核表

java - 如何在 Hibernate 中的同一实体上使用不同的验证规则?

Java - 带有ExecutorService的多线程爬虫

java - 我需要为聊天客户端分配多少个线程?

java - 使用 start.spring.io 的 spring 项目 stub 在没有 xml-conf 的情况下交付 jsp View 的最快方法