我的代码出现以下错误。
Hibernate: insert into EMPLOYEE (FIRST_NAME, LAST_NAME, SALARY, ID) values (?, ?, ?, ?)
Hibernate: update CERTIFICATE set ID=? where ID=?
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:81)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63)
这是我的代码。
Employee.java
@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@Column(name="ID")
private int id;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
@Column(name="SALARY")
private int salary;
@OneToMany
@JoinColumn(name = "ID")
@OrderBy("name ASC")
private SortedSet<Certificate> certificates;
//getters and setters
}
Certificate.java
@Entity
@Table(name="CERTIFICATE")
public class Certificate implements Comparable <Certificate>{
@Id
@Column(name="ID")
private int id;
@Column(name="CERTIFICATE_NAME")
private String name;
//generate getters and setters
}
我正在尝试使用以下代码添加用户。
ManageEmployee ME = new ManageEmployee();
/* Let us have a set of certificates for the first employee */
TreeSet set1 = new TreeSet();
set1.add(new Certificate("MCA"));
set1.add(new Certificate("MBA"));
set1.add(new Certificate("PMP"));
/* Add employee records in the database */
Integer empID1 = ME.addEmployee("Manoj", "Kumar", 4000, set1);
并使用我的addEmployee方法,我有以下代码
Employee employee = new Employee(fname, lname, salary);
employee.setCertificates(cert);
employeeID = (Integer) session.save(employee);
请建议我应该从我的最终改变这一点来解决这个问题。
答案 0 :(得分:0)
问题是因为默认情况下未设置id属性。我不得不更新我的代码以包含GeneratedValue注释并解决了这个问题。
@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private int id;