Why is the hibernate call deleted during an update transaction?

I see exceptions in my project log and I cannot reproduce the problem.

This happens from time to time (not always) when I try to update the client using Hibernate. It seems, based on the information that is present in the logs, that hibernate calls the delete method on the update transaction .

Any idea on why this is happening?

Magazine

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 // Is this because the client has been deleted by hibernate?
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:47)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2707)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2911) // This is called by hibernate
    at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.project.dao.ClientDAO.updatetx(ClientDAO.java:138)
    at com.project.bl.ClientsBL.updateClient(ClientsBL.java:2291) // This is the method I call
    at [other non hibernate code]

ClientDAO

// ...
public class ClientDAO {
    // ...    
    public Object updatetx(Object instance) throws Exception{
        Session session = getSession();
        Transaction tx = session.beginTransaction();
        session.update(instance);
        tx.commit(); // This is line ClientDAO.java:138

        return instance;
    }
    // ...
}

ClientsBL

//..
public class ClientsBL {
    // ...
    public void updateClient(Client client) {
        // ...
        clientDAO.updatetx(client); // This is ClientsBL.java:2291
        // ...
    }
    // ...    
}

EDIT : Adding Multiple Mappings and Information:

HBM:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.project.model.client.Client" table="CLIENT" dynamic-update="true">
        <id name="clientId" type="java.lang.Long">
            <column name="CLIENT_ID" />
            <generator class="native" />
        </id>

        <property name="countryId" type="java.lang.Long">
            <column name="COUNTRY_ID" />
        </property>

        <property name="name" type="string">
            <column name="NAME" />
        </property>

        <!-- ... many other columns mapped in the same way -->
    </class>
</hibernate-mapping>

Client.java has fields + getters + seters (no annotations)

Spring:

<bean id="clientsBL"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref bean="transactionManager" />
    </property>
    <property name="target">
        <ref bean="clientsBLTarget" />
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_SUPPORTS,-Exception</prop>
        </props>
    </property>
</bean>

<bean id="clientsBLTarget"
    class="com.project.bl.ClientsBL">
    <property name="clientDAO"><ref bean="clientDAO" /></property>
    <property name="clientDataDAO" ref="clientDataDAO" />
    <!-- ... more DAOs -->
</bean>
+4
source share
3 answers

, , . : unexpected row count from update [0]; actual row count: 0; expected: 1

, :

  • "" bean IOC
  • \ bean . , bean .
  • bean. , bean .
  • "" bean IOC.. , bean , , .
  • saveOrUpdate , "" bean.

, insert (save) , : unexpected row count from update [0]; actual row count: 0; expected: 1

, hibernate , , , bean, expected: 1, 1 , ​​. , actual row count: 0.

, , IOC, null.

+1

, , , .

, , Hibernate delete , orphanRemoval . , , ( SQL , ).

StaleStateException, , , , ( ), , Hibernate , , . , ( / ..).

0

You can enable debug level logging in sleep mode and verify that something is deleted. It can also be a CLIENT_ID value that is not in db, can check the same thing during debugging that before line 138 it exists.

0
source

Source: https://habr.com/ru/post/1656814/


All Articles