public void AddOperationRecord(OperationRecord record) { int month = 1;
OperationRecord OR;
for (int k = 1; k <= 4; k++) { for (int i = 1; i <= 3; i++,month++) 
{

 OR = new OperationRecord();

OR.setYearmonth(new Integer(month).toString());System.out.println(OR.getYearmonth());
OR.setYear(record.getYear());
               this.getHibernateTemplate().save(OR);
               this.getSession().flush();
              
if (k == 1&&i==3)
{  OR=new OperationRecord();    OR.setYearmonth("一季度"); OR.setYear(record.getYear());
this.getHibernateTemplate().save(OR);System.out.println(OR.getYearmonth());this.getSession().flush();}
if (k == 2&&i==3)
{  OR=new OperationRecord(); OR.setYearmonth("二季度"); OR.setYear(record.getYear());
this.getHibernateTemplate().save(OR);System.out.println(OR.getYearmonth());this.getSession().flush();}
if (k == 3&&i==3)
{  OR=new OperationRecord(); OR.setYearmonth("三季度"); OR.setYear(record.getYear());
this.getHibernateTemplate().save(OR);System.out.println(OR.getYearmonth());this.getSession().flush();}
if (k == 4&&i==3)
{  OR=new OperationRecord(); OR.setYearmonth("四季度"); OR.setYear(record.getYear());
this.getHibernateTemplate().save(OR);System.out.println(OR.getYearmonth());this.getSession().flush();}

}
}
OR=new OperationRecord();
OR.setYearmonth("全年");
OR.setYear(record.getYear());
this.getHibernateTemplate().save(OR);
System.out.println(OR.getYearmonth());
this.getSession().flush();
}}代码比较多但是都是重复的 很简单请仔细看看 其实就是连续建立17条记录 这些记录分别是
1,2,3,一季度,4,5,6.,全年
这些记录的顺序绝对不能变一变以后的算法全错现在有个非常BUG的问题 就是hibernate存入的顺序和数据库的不同
打印结果是:
1
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
2
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
3
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select hibernate_sequence.nextval from dual
一季度
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
4
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
5
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
6
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select hibernate_sequence.nextval from dual
二季度
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
7
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
8
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
9
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select hibernate_sequence.nextval from dual
三季度
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
10
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
11
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
12
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select hibernate_sequence.nextval from dual
四季度
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select hibernate_sequence.nextval from dual
全年
Hibernate: insert into Equmanage.T_OperationRecord (year, yearmonth, operformonth, afterrepair, totaloper, mainttime, notes, operatorid) values (?, ?, ?, ?, ?, ?, ?, ?)完全按顺序来的感觉一点毛病都没有但是数据库是:
2,3,一季度,4,5,6.。全年,1
就把第一个数据挪到最后了整了一上午没明白为什么 直闹心 而且最BUG 的是它有时正常有时不正常
不正常大约占1/3  试了很多次没发现规律 好像就是一定几率
大家帮帮忙吧

解决方案 »

  1.   

    我的最后一招是调用17次DAO方法然后 判断在server层写
    这样感觉在BUG也没问题了 但是真不想这样写 
    而且我也想知道为什么
      

  2.   

    这得多慢呀,hql语句使用order by排序一下就好了。
      

  3.   

    当然也可以在jsp页面上根据数据显示不根据顺序显示 然后算法也是 
    但是我主要想问是为什么会这样 和根据这个方法的解决方法 
    别的解决方法当然很多
      

  4.   

    你把 所有的 this.getSession().flush(); 都删掉   在最后一个if 执行完后  
    加一句 this.getSession().flush();  试试  !!
      

  5.   

    这是最早就这么写的  然后不好使才每次都flush 但还不好使
      

  6.   

    你有id吧 估计是你的ID在数据库的顺序不对 但是比如2,3,4,5,6,1
    比先把id排一下看看