当session中出现两个相同标示的(相同主键)的对象,一个是持久态,一个是瞬时态,想更新瞬时态对象到数据库,如果不做处理,则报出异常,session中出现两个相同标示的不同对象异常。处理方法。(业务层代码,省去了dao层)

一:通过hibernateTemplate获取映射seession(模拟)

 /**
*
* @Title: updateTest
* @Description: TODO(这里用一句话描述这个方法的作用)
* @author 尚晓飞
* @date 2014-9-19 下午7:42:36
* @see org.ledger.service.IntercoursePersionService#updateTest()
*/
@Override
public void updateTest() {
// TODO Auto-generated method stub
//持久态的对象
IntercoursePersion intercoursePersion=intercoursePersionDao.find("1");
String idString=intercoursePersion.getIntercoursePersionId(); //相当于临时态的新对象,与持久态有相同的主键
IntercoursePersion newobj=new IntercoursePersion();
newobj.setIntercoursePersionId(idString);
newobj.setName("黄天在上"); //获取hibernate.session对象,将持久态的对象变成游离态。然后修改临时态。通过spring整合的hibernateTemplate获取session
Session session=intercoursePersionDao.getHibernateTemplate().getSessionFactory().openSession();
session.beginTransaction();
session.evict(intercoursePersion);
session.update(newobj);
session.getTransaction().commit(); }

二:通过hibernateTemplate将持久态转游离态,保存瞬时态。

 /**
*
* @Title: updateTest
* @Description: TODO(这里用一句话描述这个方法的作用)
* @author 尚晓飞
* @date 2014-9-19 下午7:42:36
* @see org.ledger.service.IntercoursePersionService#updateTest()
*/
@Override
public void updateTest() {
// TODO Auto-generated method stub
//持久态的对象
IntercoursePersion intercoursePersion=intercoursePersionDao.find("1");
String idString=intercoursePersion.getIntercoursePersionId(); //相当于临时态的新对象,与持久态有相同的主键
IntercoursePersion newobj=new IntercoursePersion();
newobj.setIntercoursePersionId(idString);
newobj.setName("黄天在上"); //获取hibernateTemlate对象,将持久态的对象变成游离态,然后保存瞬时态
HibernateTemplate hibernateTemplate=intercoursePersionDao.getHibernateTemplate();
hibernateTemplate.evict(intercoursePersion);
hibernateTemplate.update(newobj); }

三:以上简单业务不错,复杂业务报错。有待解决。

  

org.springframework.orm.hibernate3.HibernateTemplate的更多相关文章

  1. org.springframework.orm.hibernate3.HibernateSystemException:

    org.springframework.orm.hibernate3.HibernateSystemException: The database returned no natively gener ...

  2. Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider停住了

    2015.1.24进行了服务器的搬家,搬家后,更换了新的IP,导致新的IP访问以前IP的数据库服务无法成功Initializing connection provider: org.springfra ...

  3. org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑解决办法

    在项目中使用了SSH框架(Struts2 + Spring3+ Hibernate3),applicationContext中配置了sessionFactory <bean id="s ...

  4. org.springframework.orm.hibernate3.support.OpenSessionInViewFilter作用

    在Spring与Hibernate集成时在web.xml要加入这样的过滤器: <filter> <filter-name>openSessionInView</filte ...

  5. Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.HibernateTemplate

    1.错误描述 严重: Context initialization failed org.springframework.beans.factory.CannotLoadBeanClassExcept ...

  6. applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found

    applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found 解决办法: 1 ...

  7. hibernate4 , spring3 使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 报错 Implementing class

    错误代码如下 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with ...

  8. java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryBean

    Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryB ...

  9. org.springframework.orm.hibernate3.LocalSessionFactoryBean

    Spring整合hibernate在配置sessionFactory时, 启动总是报出javax.transaction.TransactionManager找不到. 原因是:缺少jar包,jta-1 ...

随机推荐

  1. dns之缓存。

    1.浏览器缓存.这里以chrome为例.在chrome上输入:chrome://net-internals/#dns 可以查看chrome浏览器的dns缓存信息. 这样. 2.windows系统缓存. ...

  2. UI自动化测试框架之Selenium关键字驱动

    一.原理及特点 1. 关键字驱动测试是数据驱动测试的一种改进类型 2. 主要关键字包括三类:被操作对象(Item).操作(Operation)和值(value),用面向对象形式可将其表现为Item.O ...

  3. ipod不识别命令

    root@mx6q:~# history 0 sync 1 reboot 2 cd /usr/app/ 3 ls 4 cd extend-sysfs/ 5 ls 6 cd etc/ 7 ls 8 cd ...

  4. 2017-2018-1 JaWorld 第四、五周作业

    2017-2018-1 JaWorld 第四.五周作业 两周工作内容 小组讨论并确定最终的app雏形 合作完成需求说明书 工作分工 成员 分工 比例 陈是奇 1.引言 8% 马平川 2.1-2.5 产 ...

  5. 同样的输入,为什么Objects.hash()方法返回的hash值每次不一样?

    背景 开发过程中发现一个问题,项目中用Set保存AopMethod对象用于去重,但是发现即使往set中添加相同内容的对象,每次也能够添加成功. AopMethod类的部分代码如下: public cl ...

  6. Java8 Predicate

    code: package com.qhong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; ...

  7. Gym 100712I Bahosain and Digits(开关翻转问题)

    http://codeforces.com/gym/100712/attachments 题意: 给出一串数字,每次选择连续的k个数字加上任意数(超过10就取余),最后要使得所有数字都相等,求最大的k ...

  8. cocos2d-js入门二 环境搭建二

    电脑升级到WIN10后,cocos  code IDE安装不成功. 于是搭建新的环境JetBrainsWebStorm+chrome+JetBrains IDE Support,其中JetBrains ...

  9. codeforces 200 div2 C. Rational Resistance 思路题

    C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ...

  10. Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...