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

前端 未结 30 3644
夕颜
夕颜 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:20

    Solution: In the Hibernate mapping file for the id property, if you use any generator class, for that property you should not set the value explicitly by using a setter method.

    If you set the value of the Id property explicitly, it will lead the error above. Check this to avoid this error. or It's error show when you mention in the mapping file the field generator="native" or "incremental" and in your DATABASE the table mapped is not auto_incremented Solution: Go to your DATABASE and update your table to set auto_increment

    0 讨论(0)
  • 2020-11-28 21:20

    Another way to get this error is if you have a null item in a collection.

    0 讨论(0)
  • 2020-11-28 21:22

    I encountered this problem where we had one-many relationship.

    In the hibernate hbm mapping file for master, for object with set type arrangement, added cascade="save-update" and it worked fine.

    Without this, by default hibernate tries to update for a non-existent record and by doing so it inserts instead.

    0 讨论(0)
  • 2020-11-28 21:22

    This happened to me too, because I had my id as Long, and I was receiving from the view the value 0, and when I tried to save in the database I got this error, then I fixed it by set the id to null.

    0 讨论(0)
  • 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();
        }
    
    0 讨论(0)
  • 2020-11-28 21:23

    i got the same problem and i verified this may occur because of Auto increment primary key. To solve this problem do not inset auto increment value with data set. Insert data without the primary key.

    0 讨论(0)
提交回复
热议问题