StaleStateException:批量更新从更新 [0] 返回了意外的行数;实际行数:0;预期:1

StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

我在这个网站上找到了太多的答案,但我无法解决错误。请帮我解决这个问题。 当(Transaction) tx.commit 执行时抛出异常但我不知道 why.If 我评论 tx.commit, Employee values will be inserted进入 table 但证书值不会插入证书 table.Please 检查它。

完全例外

     Hibernate: 
    /* insert mapping.set.Employee2
        */ insert 
        into
            Employee2
            (fName, lName, salary) 
        values
            (?, ?, ?)
14:47:29,983 DEBUG StringType:80 - binding 'ABC' to parameter: 1
14:47:29,984 DEBUG StringType:80 - binding 'xyz' to parameter: 2
14:47:29,984 DEBUG IntegerType:80 - binding '10000' to parameter: 3
Hibernate: 
    /* update
        mapping.set.Certificate */ update
            Certifiedemployee 
        set
            CertificateName=? 
        where
            id=?
14:47:29,996 DEBUG StringType:80 - binding 'Java' to parameter: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at mapping.set.EmployeeDetails.addEmployee(EmployeeDetails.java:53)
    at mapping.set.EmployeeDetails.main(EmployeeDetails.java:36)
14:47:29,997 DEBUG IntegerType:80 - binding '111' to parameter: 2
14:47:29,999 ERROR AbstractBatcher:51 - Exception executing batch: 
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at mapping.set.EmployeeDetails.addEmployee(EmployeeDetails.java:53)
    at mapping.set.EmployeeDetails.main(EmployeeDetails.java:36)
14:47:30,001 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at mapping.set.EmployeeDetails.addEmployee(EmployeeDetails.java:53)
    at mapping.set.EmployeeDetails.main(EmployeeDetails.java:36)
Hibernate: 
    /* create one-to-many row mapping.set.Employee2.certificates */ update
        Certifiedemployee 
    set
        employee_id=? 
    where
        id=?
14:47:30,003 DEBUG IntegerType:80 - binding '7' to parameter: 1
14:47:30,004 DEBUG IntegerType:80 - binding '111' to parameter: 2
Hibernate: 
    /* create one-to-many row mapping.set.Employee2.certificates */ update
        Certifiedemployee 
    set
        employee_id=? 
    where
        id=?
14:47:30,004 DEBUG IntegerType:80 - binding '7' to parameter: 1
14:47:30,004 DEBUG IntegerType:80 - binding '111' to parameter: 2
14:47:30,010 ERROR AbstractBatcher:51 - Exception executing batch: 
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

Class 员工

import java.util.Set;
@SuppressWarnings("rawtypes")    
public class Employee2 {     
    private int id;    
    private String firstName;    
    private String lastName;    
    private int salary;    
    private Set certificates;  
public Employee2() {    }
public Employee2(String firstName, String lastName, int salary) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.salary = salary;
}
}// Getters and setters are also present in the code

证书class

public class Certificate {    
    private int id;    
    private String name;    

    public Certificate() {  }
    public Certificate(int id, String name) {
        this.id = id;
        this.name = name;
    }
}// Getters and setters are also present in the code

主要class

public class EmployeeDetails {
    private static SessionFactory factory=null;
    public static void main(String[] args) {
        try{
            factory=new Configuration().configure().buildSessionFactory();
            System.out.println("Created session factory object");

        }catch(Throwable ex){
            System.err.println("Failed to create session factory object!\t"+ex);
        }
        EmployeeDetails emp=new EmployeeDetails();
        Set<Certificate> set1=new HashSet<Certificate>();
        Certificate c1=new Certificate(111, "Java");
        set1.add(c1);
        Integer empid1=emp.addEmployee("ABC","xyz",10000, set1);
        System.out.println("The id is"+empid1);
    }
    private Integer addEmployee(String firstName, String lastName, int salary,Set<Certificate> set1) {
        Integer id=null;
        Session session=factory.openSession();
        Transaction tx=null;
        try {
            tx=session.beginTransaction();
            Employee2 employee=new Employee2(firstName, lastName, salary);
            employee.setCertificates(set1);
            employee.setId(0);
            id=(Integer) session.save(employee);
            tx.commit();            
        } catch(HibernateException e) {
            e.printStackTrace();
        }finally{
            session.flush();
            session.close();
        }
        return id;
    }
}

Employee2.hbm.xml

<hibernate-mapping>
    <class name="mapping.set.Employee2" table="Employee2">
    <meta attribute="class-description">
        this class contains Employee certificate and other details
    </meta>
    <id name="id" type="int" column="empId">
    <generator class="native"></generator>
    </id>

    <set name="certificates" table="Certifiedemployee" cascade="save-update" >
    <key column="employee_id"/>
    <one-to-many class="mapping.set.Certificate"/>
    </set>

    <property name="firstName" column="fName" type="string"/>
    <property name="lastName" column="lName" type="string"/>
    <property name="salary" column="salary" type="int"/>
    </class>

    <class name="mapping.set.Certificate" table="Certifiedemployee">
    <meta attribute="class-description">
        This class contains certificate records 
    </meta>
    <id name="id" column="id" type="int">
    <generator class="native"/>
    </id>
    <property name="name" column="CertificateName" type="string"></property>
    </class>

</hibernate-mapping>

hibernate.cfg.xml

<session-factory>
        <!--Database connection Settings -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/temp</property>
        <property name="connection.username">siddu</property>
        <property name="connection.password">siddu</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>

        <!-- <property name="hbm2ddl.auto">create</property> -->
        <!-- Use XML-based mapping metadata --> 
        <mapping resource="employee2.hbm.xml"/>
 </session-factory>

提前致谢

构造函数设置id值出现问题

public Certificate(int id, String name) {
        this.id=id;
        this.name = name;
    }

在证书 class 中并自动递增 id 值

<id name="id" column="id" type="int">
    <generator class="native"/>
    </id>

在 Employee2.hbm.xml file.Only 中,您必须 use.After 删除

<generator class="native"/>

从 Employee2.hbm.xml 文件中,我在控制台中得到以下结果:

Hibernate: 
    /* insert mapping.set.Employee2
        */ insert 
        into
            Employee2
            (fName, lName, salary) 
        values
            (?, ?, ?)
22:03:42,850 DEBUG StringType:80 - binding 'ABC' to parameter: 1
22:03:42,850 DEBUG StringType:80 - binding 'xyz' to parameter: 2
22:03:42,850 DEBUG IntegerType:80 - binding '10000' to parameter: 3
Hibernate: 
    /* get current state mapping.set.Certificate */ select
        certificat_.id,
        certificat_.CertificateName as Certific2_1_ 
    from
        Certifiedemployee certificat_ 
    where
        certificat_.id=?
22:03:42,850 DEBUG IntegerType:80 - binding '111' to parameter: 1
Hibernate: 
    /* insert mapping.set.Certificate
        */ insert 
        into
            Certifiedemployee
            (CertificateName, id) 
        values
            (?, ?)
22:03:42,866 DEBUG StringType:80 - binding 'Java' to parameter: 1
22:03:42,866 DEBUG IntegerType:80 - binding '111' to parameter: 2
Hibernate: 
    /* create one-to-many row mapping.set.Employee2.certificates */ update
        Certifiedemployee 
    set
        employee_id=? 
    where
        id=?
22:03:42,866 DEBUG IntegerType:80 - binding '1' to parameter: 1
22:03:42,866 DEBUG IntegerType:80 - binding '111' to parameter: 2
The id isnull