您的位置:首页 > 运维架构 > 网站架构

s2sh购物网站的版本控制version,遇到update就出错

2016-06-04 20:34 441 查看
我用s2sh做购物网站,里面用到了版本控制version,save和delete没有问题,但update就显示出错。

我一运行就点update,所以不存在已有一个线程在。我觉得是这个问题,不知对不对。

<tx:method name="*" propagation="REQUIRED"/>我的数据库version字段都为0。

以下是错误代码:

 org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Object of class [com.fangyun.entity.Product] with identifier [0]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted
by another transaction (or unsaved-value mapping was incorrect): [com.fangyun.entity.Product#0] org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.fangyun.entity.Product#0]

后来发现不是这个问题<tx:method name="*" propagation="REQUIRED"/>

当所有问题都揉在一起,是看不清出错的地方的。

因为问题涉及到s2sh框架的问题,hibernate4的版本问题,spring的问题,version与其它部件的组合问题,mysql的问题。

这个问题卡了我一个星期的时间,不过,有一天我灵光一闪。

何不用一种简单的方法来测试错误呢?

我建了一个version项目,只用hibernate框架和version测试。

文件有:Version.java,Version.hbm.xml,hibernate.cfg.xml,VersionService.java

当然还有所需要的包。

  Configuration config=new Configuration().configure();

  SessionFactory sessionFactory=config.buildSessionFactory();

  Session session=sessionFactory.openSession();

  Transaction tx=null;

  tx=session.beginTransaction();

  session.update(version2);

  tx.commit();

却出现了同样的错误提示,同样数据库version字段都为0,一下了去除了很多错误方向。

问题到底到哪呢?我翻着老师上课时的笔记,突然发现自己范了一个可笑的错误。

问题聚焦到这一句session.update(version2);

做update前,需要做一件事,我把这件事给忘了,是不是有点神秘?

正确的代码为:

  Configuration config=new Configuration().configure();

  SessionFactory sessionFactory=config.buildSessionFactory();

  Session session=sessionFactory.openSession();

  Transaction tx=null;

  tx=session.beginTransaction();  

  Version version2=(Version)session.load(Version.class,new Long(3));

  version2.setPassword("0000000000");  

  session.update(version2);

  tx.commit();

忘了写load了!!!!!

当我把正确的代码写好后,右键运行java应用程序。一切问题解决,version字段变为1。

转到我s2sh购物网站,改后,运行,原有的错误提示没了,但出现一条空指针异常。

这难不倒我,测试到原来是id没传过来,注:update()里。

但测试后在updateInput(),是有id值的,我还在里面设了product.setId(tp.getId());,但是没有用。

这涉及到页面与action之间的传值的问题。

我改了这个<form action="product_update.action?id=${id}"

结果显示错误。

我又打开老师的源代码,思考着,最后在文件product_updateInput.jsp里加了句<s:hidden name="id"/>

空指针问题解决。好了问题都解决了,休息,休息一会儿……

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息