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

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

    This happens when you declared the JSF Managed Bean as

    @RequestScoped;
    

    when you should declare as

    @SessionScoped;
    

    Regards;

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

    I got this error when I tried to update an object with an id that did not exist in the database. The reason for my mistake was that I had manually assigned a property with the name 'id' to the client side JSON-representation of the object and then when deserializing the object on the server side this 'id' property would overwrite the instance variable (also called 'id') that Hibernate was supposed to generate. So be careful of naming collisions if you are using Hibernate to generate identifiers.

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

    In our case we finally found out the root cause of StaleStateException.

    In fact we were deleting the row twice in a single hibernate session. Earlier we were using ojdbc6 lib, and this was ok in this version.

    But when we upgraded to odjc7 or ojdbc8, deleting records twice was throwing exception. There was bug in our code where we were deleting twice, but that was not evident in ojdbc6.

    We were able to reproduce with this piece of code:

    Detail detail = getDetail(Long.valueOf(1396451));
    session.delete(detail);
    session.flush();
    session.delete(detail);
    session.flush();
    

    On first flush hibernate goes and makes changes in database. During 2nd flush hibernate compares session's object with actual table's record, but could not find one, hence the exception.

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

    As Julius says this happens when an update Occurs on an Object that has its children being deleted. (Probably because there was a need for an update for the whole Father Object and sometimes we prefer to delete the children and re -insert them on the Father (new , old doesnt matter )along with any other updates the father could have on any of its other plain fields) So ...in order for this to work delete the children (within a Transaction) by calling childrenList.clear() (Dont loop through the children and delete each one with some childDAO.delete(childrenList.get(i).delete())) and setting @OneToMany(cascade = CascadeType.XXX ,orphanRemoval=true) on the Side of the Father Object. Then update the father (fatherDAO.update(father)). (Repeat for every father object) The result is that children have their link to their father stripped off and then they are being removed as orphans by the framework.

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

    Without code and mappings for your transactions, it'll be next to impossible to investigate the problem.

    However, to get a better handle as to what causes the problem, try the following:

    • In your hibernate configuration, set hibernate.show_sql to true. This should show you the SQL that is executed and causes the problem.
    • Set the log levels for Spring and Hibernate to DEBUG, again this will give you a better idea as to which line causes the problem.
    • Create a unit test which replicates the problem without configuring a transaction manager in Spring. This should give you a better idea of the offending line of code.

    Hope that helps.

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

    its happen when you try to delete the same object and then again update the same object use this after delete

    session.clear();

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