---
title: "【异常】net.sf.hibernate.NonUniqueObjectException"
author: "fanjf"
date: "2012-03-22T16:06:24Z"
tags: []
categories: ["开源软件"]
slug: "net-sf-hibernate-nonuniqueobjectexception"
draft: false
lastmod: "2012-03-22T16:07:15Z"
id: 1461502
url: "https://fanjf.iteye.com/blog/1461502"
---

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");

//假设User是一个持久对象，有一个name属性  
user1.setName("anray1");  
user2.setName("anray2");  
session.update(user1);  
session.update(user2);

这样的代码，我们一般不可能会这样写，但有时，我们回写一个专门的类来管理sessionFactory和session，如：  
package com.cpic.p17.pf.dao;

import org.hibernate.HibernateException;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  
import org.hibernate.classic.Session;

/\**  
 * Hibernate DAO实现  
 * @author fanjf  
 * @date 2012-3-22  
 \*/  
public class HibernateDao {  
    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();）  
    /\**  
     \*更新一个对象到数据库中，使用完后要自己调用closeSession()  
     \*/  
    public static void update(Object object) throws HibernateException {  
        Session session1 = getSession();  
        Transaction transaction1 = session1.beginTransaction();  
        try {  
            session1.clear();//更新对象前，清空session

            session1.update(object);  
            transaction1.commit();  
        } catch (HibernateException ex) {  
            if (transaction1 != null)  
                transaction1.rollback();  
            closeSession();  
            throw ex;  
        }  
    }