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

Hibernate StaleStateException:批处理更新从更新[0]返回了意外的行计数;实际行数:0;预期:1,hibernate,hibernate-mapping,hibernate-onetomany,Hibernate,Hibernate Mapping,Hibernate Onetomany,我在这个网站上找到了太多的答案,但我无法解决这个错误。请帮我解决这个问题。 执行(事务)tx.commit时引发异常,但我不知道原因。如果我注释tx.commit,员工值将插入到表中,但证书值不会插入到证书表中。请检查它 完全例外 班级员工 证书类 主类 公共类EmployeeDetails{ 私有静态SessionFactory=null; 公共静态void main(字符串[]args){ 试一试{ 工厂=新配置().configure().buildSessionFactory(); Sy

我在这个网站上找到了太多的答案,但我无法解决这个错误。请帮我解决这个问题。 执行(事务)tx.commit时引发异常,但我不知道原因。如果我注释tx.commit,员工值将插入到表中,但证书值不会插入到证书表中。请检查它

完全例外 班级员工 证书类 主类
公共类EmployeeDetails{
私有静态SessionFactory=null;
公共静态void main(字符串[]args){
试一试{
工厂=新配置().configure().buildSessionFactory();
System.out.println(“创建的会话工厂对象”);
}捕获(可丢弃的ex){
System.err.println(“创建会话工厂对象失败!\t”+ex);
}
EmployeeDetails emp=新的EmployeeDetails();
Set set1=新的HashSet();
证书c1=新证书(111,“Java”);
set1.添加(c1);
整数empid1=emp.addEmployee(“ABC”,“xyz”,10000,set1);
System.out.println(“id为”+empid1);
}
私有整数addEmployee(字符串firstName、字符串lastName、整数salary、Set set1){
整数id=null;
Session Session=factory.openSession();
事务tx=null;
试一试{
tx=session.beginTransaction();
Employee2 employee=新员工2(姓、名、薪);
员工证书(set1);
employee.setId(0);
id=(整数)session.save(雇员);
tx.commit();
}捕获(休眠异常e){
e、 printStackTrace();
}最后{
session.flush();
session.close();
}
返回id;
}
}
Employee2.hbm.xml

此类包含员工证书和其他详细信息
此类包含证书记录
hibernate.cfg.xml

org.hibernate.dialogue.mysql5dialogue
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/temp
锡杜
锡杜
真的
真的
真的

提前感谢

由于构造函数设置了id值,所以出现了问题

public Certificate(int id, String name) {
        this.id=id;
        this.name = name;
    }
在证书类中,自动递增id值

<id name="id" column="id" type="int">
    <generator class="native"/>
    </id>
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
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;
    }
}
<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>
<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>
public Certificate(int id, String name) {
        this.id=id;
        this.name = name;
    }
<id name="id" column="id" type="int">
    <generator class="native"/>
    </id>
<generator class="native"/>
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