日期:2014-05-19  浏览次数:20615 次

hibernate对象写入问题
public boolean doRecharge(String account) {
String userid = ((Integer)ActionContext.getContext().getSession().get("userId")).toString();
System.out.println("userId====="+userid);
EmUser user = userDAO.findById(Integer.parseInt(userid));
EmAccount accountEntity = (EmAccount) accountDAO.findByProperty("emUser",user).get(0);
if (accountEntity.getAccountAmount()==null) {
accountEntity.setAccountAmount(Double.parseDouble(account));
}else{
accountEntity.setAccountAmount(accountEntity.getAccountAmount()+Double.parseDouble(account));
}
accountDAO.merge(accountEntity);
return true;
}
为什么写入不了数据库?各位大神乱入了。。。
补充一下:什么错误都没有报出来,就是写不进数据库。。。

------解决方案--------------------
可能是spring的事务你没有加上 看看你的spring的配置文件 有没有包含你service的包 或者相应的类上加没加事务的注解
------解决方案--------------------
事务是否提交?
如果没有用到事务管理,那可能需要commit;
ActionContext.getContext().getSession().getTransaction().commit();

hibernate配置中也可以设置自动提交 setAutoCommit(false)
------解决方案--------------------
没有事务提交哦。。。。
------解决方案--------------------
accountDAO.merge(accountEntity);

把merge换成flush试一下。

public boolean doRecharge(String account) {
String userid = ((Integer)ActionContext.getContext().getSession().get("userId")).toString();
System.out.println("userId====="+userid);
EmUser user = userDAO.findById(Integer.parseInt(userid));
EmAccount accountEntity = (EmAccount) accountDAO.findByProperty("emUser",user).get(0);
if (accountEntity.getAccountAmount()==null) {
accountEntity.setAccountAmount(Double.parseDouble(account));
}else{
accountEntity.setAccountAmount(accountEntity.getAccountAmount()+Double.parseDouble(account));
}
accountDAO.persist(accountEntity);
//accountDAO.merge(accountEntity);
accountDAO.flush()
return true;
}
------解决方案--------------------
这种情况的发生我所遇到的有两种可能:

1、事务配置的问题,虽然不会报错,不过就是不会执行。

2、你在配置Hibernate文件的时候,针对你所操作的对象属性设置了update为false.当然这是比较老的写法了。