Hibernate, Java

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected

Got this error today while working with Hibernate. I am not sure what all scenarios can cause this error. In my case, it was the value of the ‘id’ property in Hibernate mapping file that was causing this problem. Basically, 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.

<hibernate-mapping>

    <class name="com.devesh.database.model.Student" table="Student" >
        <id name="studentId" type="java.lang.Long">
            <column name="StudentID"/>
            <generator class="native"/>
        </id>
        <property name="studentName" type="java.lang.String">
            <column name="StudentName"/>
        </property>
        ---
        ---
    </class>

</hibernate-mapping>

That is, for the above Hibernate mapping file, I should not set the value of ‘studentId’ field explicitly by using setter method and try to save it to the database. It will be generated automatically in the database.