hibernate的问题
delete error
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [bean.StuBean#1]
我在做修改操作
public boolean updateStu(StuBean stu)
{
Session s=HibernateSessionFactory.getSession();
Transaction tran=s.beginTransaction();
String sql= "from StuBean where id= ' "+stu.getId()+ " ' ";
System.out.println(stu.getId()+stu.getName()+stu.getSex()+stu.getTel());
Query q=s.createQuery(sql);
List <StuBean> l=q.list();
System.out.println(l);
if(!l.isEmpty())
{
try {
s.update(stu);
tran.commit();
} catch (
HibernateException e) {
// TODO 自动生成 catch 块
System.out.println( "update error ");
e.printStackTrace();
}
}
else
{
s.close();
return true;
}
s.close();
return false;
}
上面的错误是什么意思啊
------解决方案--------------------session 缓存中已经存在你要更新的对象了
先清除掉对象
s.clear() ;
s.update(stu);