net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: .........
这样的问题不知道大家有没有遇到?
SessionFactory sessionFactory= new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
User user1=new User("anray");
User user2=new User("anray");
user1.setName("anray1");//假设User是一个持久对象,有一个name属性
user2.setName("anray2");
session.update(user1);
session.update(user2);
这样就会出现上面的异常
看上去上面的代码有点傻,我们编程中不可能出现这样的情况。
但是有一种情况是经常用到的,就是写一个专门的类来管理sessionFactory和session,如下
/*
* Examination System version 0.1
* Data persist layer(Using Hibernate 2.1.2)
* Created on 2004-4-9 11:58:12
*/
package bts.hibernate;
/**
* @author Anray
*/
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUse
{
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}
public static final ThreadLocal threadLocal = new ThreadLocal();
/**得到当前线程的session */
public static final Session getSession() throws HibernateException {
Session se = (Session) threadLocal.get();
// Open a new Session, if this Thread has none yet
if (se == null) {
se = sessionFactory.openSession();
threadLocal.set(se) ;
}
return se;
}
/**关闭当前线程的session */
public static final void closeSession() throws HibernateException {
Session s = (Session) threadLocal.get();
threadLocal.set(null);
if (s != null)
s.close();
}
/**
*保存一个对象到数据库中,使用完后要自己调用closeSession()
*/
public static void create(Object object)
throws HibernateException{
Session session1 = getSession();
Transaction transaction1 = session1.beginTransaction();
try{
session1.save(object);
transaction1.commit();
}catch(HibernateException ex){
if(transaction1!=null)transaction1.rollback();
closeSession();
throw ex;
}
}
/**
*更新一个对象到数据库中,使用完后要自己调用closeSession()
*/
public static void update(Object object)
throws HibernateException{
Session session1 = getSession();
Transaction transaction1 = session1.beginTransaction();
try{
session1.update(object);
transaction1.commit();
}catch(HibernateException ex){
if(transaction1!=null)transaction1.rollback();
closeSession();
throw ex;
}
}
}
如果在web编程时用这个类的方法来更新一个user对象,在调用了HibernateUse.update(user)后,没有接着调用HibernateUse.closeSession(),其实为了节省资源也不提倡每次操作都调用HibernateUse.closeSession()关闭session;如果对相同的user对象进行又一次的更新时,碰巧容器又分配了同一个thread进行处理,那么得到的也是同一个session,这样就出现了上面的提到了问题,session里的两个user的识别id一样,但内容不一样。
为了解决这个问题可以在,update前清空session(不是关闭,这样资源消耗比较少),修改后的公用类如下:(就是在update()方法里添加了session1.clear();)
/*
* Examination System version 0.1
* Data persist layer(Using Hibernate 2.1.2)
* Created on 2004-4-9 11:58:12
*/
package bts.hibernate;
/**
* @author Anray
*/
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUse
{
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
}
public static final ThreadLocal threadLocal = new ThreadLocal();
/**得到当前线程的session */
public static final Session getSession() throws HibernateException {
Session se = (Session) threadLocal.get();
// Open a new Session, if this Thread has none yet
if (se == null) {
se = sessionFactory.openSession();
threadLocal.set(se);
}
return se;
}
/**关闭当前线程的session */
public static final void closeSession() throws HibernateException {
Session s = (Session) threadLocal.get();
threadLocal.set(null);
if (s != null)
s.close();
}
/**
*保存一个对象到数据库中,使用完后要自己调用closeSession()
*/
public static void create(Object object)
throws HibernateException{
Session session1 = getSession();
Transaction transaction1 = session1.beginTransaction();
try{
session1.save(object);
transaction1.commit();
}catch(HibernateException ex){
if(transaction1!=null)transaction1.rollback();
closeSession();
throw ex;
}
}
/**
*更新一个对象到数据库中,使用完后要自己调用closeSession()
*/
public static void update(Object object)
throws HibernateException{
Session session1 = getSession();
Transaction transaction1 = session1.beginTransaction();
try{
session1.clear();
session1.update(object);
transaction1.commit();
}catch(HibernateException ex){
if(transaction1!=null)transaction1.rollback();
closeSession();
throw ex;
}
}
}
第一次写东西,大家多指教
a different object with the
最新推荐文章于 2022-04-29 09:45:04 发布