Hibernate - Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1

前端 未结 30 3854
夕颜
夕颜 2020-11-28 20:47

I get following hibernate error. I am able to identify the function which causes the issue. Unfortunately there are several DB calls in the function. I am unable to find the

30条回答
  •  一向
    一向 (楼主)
    2020-11-28 21:22

    This problem mainly occurs when we are trying to save or update the object which are already fetched into memory by a running session. If you've fetched object from the session and you're trying to update in the database, then this exception may be thrown.

    I used session.evict(); to remove the cache stored in hibernate first or if you don't wanna take risk of loosing data, better you make another object for storing the data temp.

         try
        {
            if(!session.isOpen())
            {
                session=EmployeyDao.getSessionFactory().openSession();
            }
                tx=session.beginTransaction();
    
            session.evict(e);
            session.saveOrUpdate(e);
            tx.commit();;
            EmployeyDao.shutDown(session);
        }
        catch(HibernateException exc)
        {
            exc.printStackTrace();
            tx.rollback();
        }
    

提交回复
热议问题