我用现在需要的一次更新数据表中的很多条数据,在我更新数据时发现了一个问题。我第一次启动tomcat可以将数据更新如数据库中。但我将数据库中的数据提取出来,不能显示我更新的数据,而事实上我数据库中的数据是已经更新了的,这一点我在数据库中可以看到已更新的数据。但是我接着又一次去更新数据,数据不能更新入数据库中。我重启一下tomcat第一次更新的数据可以在我的页面上看到。
上面的问题是,我重启一下tomcat,可以看到我先前更新的数据,并且我可以更新一次数据如数据库中,但我第二次刷新页面不能看到我刚才更新的数据,也不能再次更新数据到数据库中。那位仁兄有好的建议可以改掉这个bug,希望不吝赐教。
上面的问题是,我重启一下tomcat,可以看到我先前更新的数据,并且我可以更新一次数据如数据库中,但我第二次刷新页面不能看到我刚才更新的数据,也不能再次更新数据到数据库中。那位仁兄有好的建议可以改掉这个bug,希望不吝赐教。
List<Danyuanxiuzheng> dyxzlist = new ArrayList<Danyuanxiuzheng>();
public ActionForward getXzs(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
JizhunjiaForm jizhunjiaForm = (JizhunjiaForm) form;
int ldxxID = Integer.parseInt(request.getParameter("ldxxID"));
List<Fangwumingxi> fwmxlist = fwmxbiz.getFwmxs(ldxxID);
Iterator<Fangwumingxi> fwmxit = fwmxlist.iterator();
Fangwumingxi fwmx = null;
while(fwmxit.hasNext()){
fwmx = fwmxit.next();
dyxzlist.add(xzbiz.getXiuzhengxishu(fwmx));
} request.setAttribute("dyxzlist", dyxzlist);
return mapping.findForward("getXzs");
}
//修改单元的修正体系
public ActionForward updateXs(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int danyuanID = -1;
String gebiexzxs = null;
String fujiaxzxs = null;
boolean flag = false;
for(int i=1; i<=dyxzlist.size(); i++){
danyuanID = dyxzlist.get(i-1).getDanyuanId();
gebiexzxs = request.getParameter("gebiexzxs"+i);
fujiaxzxs = request.getParameter("fujiaxzxs"+i);
flag = xzbiz.updateXz(danyuanID, gebiexzxs, fujiaxzxs);
}
if(flag){
return mapping.findForward("updateSuccess");
}else{
return mapping.findForward("updateError");
}
}
//个别修正
String hql = "from Xiuzheng as x where 1=1 and x.codeType.codeTypeId=7 and x.fangwumingxi.fangwumingxiId=:fwmxID";
Query query = bdao.get_Session().createQuery(hql);
query.setInteger("fwmxID", danyuanID);
Xiuzheng xz = (Xiuzheng) query.setMaxResults(1).uniqueResult();
boolean flag = false;
if(xz != null){
xz.setXiuzhengxishu(gebiexzxs);
System.out.println("+++++++");
flag = bdao.updateObject(xz);
}else{
xz = new Xiuzheng();
xz.setFangwumingxi(fwmxdao.getFwmx(danyuanID));
xz.setCodeType(ctdao.getCodeType(7));
xz.setXiuzhengxishu(gebiexzxs);
xz.setXiuzhengfanwei("个别修正");
flag = bdao.saveObject(xz);
}
//附加修正
hql = "from Xiuzheng as x where 1=1 and x.codeType.codeTypeId=8 and x.fangwumingxi.fangwumingxiId=:fwmxID";
query = bdao.get_Session().createQuery(hql);
query.setInteger("fwmxID", danyuanID);
xz = (Xiuzheng) query.setMaxResults(1).uniqueResult();
if(xz != null){
xz.setXiuzhengxishu(fujiaxzxs);
System.out.println("------------");
flag = bdao.updateObject(xz);
}else{
xz = new Xiuzheng();
xz.setFangwumingxi(fwmxdao.getFwmx(danyuanID));
xz.setCodeType(ctdao.getCodeType(8));
xz.setXiuzhengxishu(gebiexzxs);
xz.setXiuzhengfanwei("附加修正");
flag = bdao.saveObject(xz);
}
return flag;
}
你这全拼音的命名法,看得晕
package linkey.dw.dao.impl;import java.util.List;import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import linkey.dw.dao.IBaseDao;/**
* @作者 邓文
* @创建日期 Mar 17, 2010
* @版本 V1.0
*/
public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao { public boolean deleteObject(Class c, Integer id) {
boolean flag = false;
try{
this.getHibernateTemplate().delete(this.getObjectbyID(c, id));
flag = true;
}catch(HibernateException e){
e.printStackTrace();
throw e;
}
return flag;
} public List getList(int first, int max) {
return null;
} public List getListbyHQL(int first, int max, String hql) {
List<Object> list = null;
try{
Query query = this.getSession().createQuery(hql);
query.setFirstResult(first);
query.setMaxResults(max);
list = query.list();
}catch(HibernateException e){
e.printStackTrace();
}
return list;
} public Object getObjectbyID(Class c, Integer id) {
return this.getHibernateTemplate().get(c, id);
} public Object getObjectbyinfo(String info) {
return null;
} public int getSum(String hql) {
int count = 0;
try{
Query query = getSession().createQuery(hql);
count = (Integer)query.setMaxResults(1).uniqueResult();
}catch(HibernateException e){
e.printStackTrace();
}
return count;
} public boolean saveObject(Object o) {
boolean flag = false;
try{
this.getHibernateTemplate().save(o);
// this.getHibernateTemplate().saveOrUpdate(o);
flag = true;
}catch(HibernateException e){
flag = false;
e.printStackTrace();
throw e;
}catch(Exception ex){
flag = false;
ex.printStackTrace();
}finally{
this.getHibernateTemplate().clear();
}
return flag;
} public boolean updateObject(Object o) {
boolean flag = false;
try{
this.getHibernateTemplate().update(o);
flag = true;
}catch(HibernateException e){
flag = false;
e.printStackTrace();
throw e;
}catch(Exception ex){
flag = false;
}
return flag;
} public List getListbyEntity(Object o, int first, int max) {
List<Object> list = null;
try{
list = this.getHibernateTemplate().findByExample(o, first, max);
}catch(HibernateException e){
e.printStackTrace();
}
return list;
} public List getObjectEntity(Object o) {
return this.getHibernateTemplate().findByExample(o);
}
public Session get_Session(){
return this.getSession();
}
public List getAllObject(String hql){
return this.getHibernateTemplate().find(hql);
}}
getHibernateTemplate().flush()。
试试吧。
你程序里没做事务控制。
你配置事务了么,这里也没有提交事务的操作。
如果你在spring中配置了事务的传播特性了,而且没有错误。这里的数据是可以提交到数据库的。
虽然数据是到数据库了,如果没有配置事务是可以回滚的!
所以你看见数据更新到数据库,但是后来又没有了。就是这个原因!!
开始事务 Transation tx = this.session.beginTransation();
提交 tx.commit();
回滚 tx.rollback();
这里有个帖子,你慢慢研究:
http://hi.baidu.com/beforedead/blog/item/75d9272ad44dba395343c1f8.html
不过在spring中配置aop来管理事务比较方便,不用再管事务这层了
专注业务就行,一般配置在xml文件中。也有注解的配置方法!