开发一个项目,上月的产品“结余”值,等于这个月的“上月结存”,所以我用定时任务quartz在1号的凌晨2点15分执行任务.
思路是先循环所以车间,然后再根据车间和上月的月份遍历所有产品,然后根据每个对象的ID号,车间名、判断本月有无此对象,没有进进行创建对象,但是出现一个现象,就是有的个别记录出现重复记录现象,而且还是间歇性的,并不是所有时候都这样,比如
上月:
id 月份 名称 结余 上月结存
1 2 齿轮 200
2 3 齿轮 200 200
3 3 齿轮 200 200这样就出现重复的2条记录。请大家帮忙啊,
/**
* 自动合计这个月的上月结存
*/
public void addLastMonthBalance() {
Calendar rightNow = Calendar.getInstance();
int thisYear = rightNow.get(Calendar.YEAR);
int thisMonth = rightNow.get(Calendar.MONTH) + 1;
int lastYear = thisYear;
int lastMonth = thisMonth -1;
if(lastMonth == 0){
lastYear = thisYear - 1;
lastMonth = 12;
}
List secList = this.searchSector();//循环所有的车间
String hql = "select s from Stats s where s.sector.id = ? and s.yearNum = ? and s.monthNum = ? and s.balance != 0";
for(Iterator iter = secList.iterator(); iter.hasNext();){
Sector sector = (Sector)iter.next();
//遍历上月的本车间的所有产品
List list = this.getSession().createQuery(hql).setParameter(0, sector.getId())
.setParameter(1, lastYear)
.setParameter(2, lastMonth)
.list();
if(list.size() > 0){ String sHql = "select s from Stats s "
+ "where s.parts.id = ? and s.sector.id = ? and s.yearNum = ? and s.monthNum = ?";
for (Iterator ite = list.iterator(); ite.hasNext();) {
Stats stats = (Stats)ite.next();//循环所有产品对象
//判断本月是否存在这个对象
Stats sta = (Stats) this.getSession().createQuery(sHql)
.setParameter(0,stats.getParts().getId())
.setParameter(1, stats.getSector().getId())
.setParameter(2, thisYear)
.setParameter(3,thisMonth)
.uniqueResult(); if(sta == null) {//如果本月不存在就创建对象,赋值
Stats newStats = new Stats();
newStats.setSector(stats.getSector());
newStats.setParts(stats.getParts());
newStats.setUpBalance(stats.getBalance());
newStats.setYearNum(thisYear);
newStats.setMonthNum(thisMonth);
newStats.setBalance(newStats.getBalanceValue());
this.getHibernateTemplate().saveOrUpdate(newStats);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().clear();
}else{
sta.setUpBalance(stats.getBalance());
sta.setBalance(sta.getBalanceValue());
this.getHibernateTemplate().update(sta);
}
}
}
}
}
思路是先循环所以车间,然后再根据车间和上月的月份遍历所有产品,然后根据每个对象的ID号,车间名、判断本月有无此对象,没有进进行创建对象,但是出现一个现象,就是有的个别记录出现重复记录现象,而且还是间歇性的,并不是所有时候都这样,比如
上月:
id 月份 名称 结余 上月结存
1 2 齿轮 200
2 3 齿轮 200 200
3 3 齿轮 200 200这样就出现重复的2条记录。请大家帮忙啊,
/**
* 自动合计这个月的上月结存
*/
public void addLastMonthBalance() {
Calendar rightNow = Calendar.getInstance();
int thisYear = rightNow.get(Calendar.YEAR);
int thisMonth = rightNow.get(Calendar.MONTH) + 1;
int lastYear = thisYear;
int lastMonth = thisMonth -1;
if(lastMonth == 0){
lastYear = thisYear - 1;
lastMonth = 12;
}
List secList = this.searchSector();//循环所有的车间
String hql = "select s from Stats s where s.sector.id = ? and s.yearNum = ? and s.monthNum = ? and s.balance != 0";
for(Iterator iter = secList.iterator(); iter.hasNext();){
Sector sector = (Sector)iter.next();
//遍历上月的本车间的所有产品
List list = this.getSession().createQuery(hql).setParameter(0, sector.getId())
.setParameter(1, lastYear)
.setParameter(2, lastMonth)
.list();
if(list.size() > 0){ String sHql = "select s from Stats s "
+ "where s.parts.id = ? and s.sector.id = ? and s.yearNum = ? and s.monthNum = ?";
for (Iterator ite = list.iterator(); ite.hasNext();) {
Stats stats = (Stats)ite.next();//循环所有产品对象
//判断本月是否存在这个对象
Stats sta = (Stats) this.getSession().createQuery(sHql)
.setParameter(0,stats.getParts().getId())
.setParameter(1, stats.getSector().getId())
.setParameter(2, thisYear)
.setParameter(3,thisMonth)
.uniqueResult(); if(sta == null) {//如果本月不存在就创建对象,赋值
Stats newStats = new Stats();
newStats.setSector(stats.getSector());
newStats.setParts(stats.getParts());
newStats.setUpBalance(stats.getBalance());
newStats.setYearNum(thisYear);
newStats.setMonthNum(thisMonth);
newStats.setBalance(newStats.getBalanceValue());
this.getHibernateTemplate().saveOrUpdate(newStats);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().clear();
}else{
sta.setUpBalance(stats.getBalance());
sta.setBalance(sta.getBalanceValue());
this.getHibernateTemplate().update(sta);
}
}
}
}
}
解决方案 »
- 求助!hibernate 多表连接查询(就两个表)
- 请问一下hibernate中怎么保存字段的默认值
- webservice webmethod 参数传值问题 急急
- 关于双向一对多关联的问题,请求帮忙
- java poi 操作Excel问题
- 奇怪问题:Spring+Hibernate事务不能自动提交等App Server停止后才向数据库提交
- Java通过jdbcOdbc访问Access数据为中时 like语句的问题.....急!
- 请问在Java中,如何编写一个菜单来调用html页面?
- ejb发布后执行servlet出现的一个异常,帮忙看看,wait.....
- 求助 键盘输入
- jsp上图片是相对路径的,但通过浏览器访问却总是同一张图片
- Hibernate登录异常,求解
比如
synchronized{.....}
在进入这段代码的时候, 加锁, 离开的时候, 解锁
呵呵,quartz是线程池的,所以不用再他身上动脑筋的,他应该不会出现重复问题;