当我尝试通过hibernate 持久层保存一些数据时出现异常,异常是
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): hbm.Employee
public void saveEmployee(Employee empValue) {
Session session = null;
Transaction tx = null;
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
Employee emp;
if (empValue.getEmpcode() != null) {
emp = (Employee) session.get(Employee.class, empValue.getEmpcode());
if (emp != null) {
System.out.println("Test");
emp.setEmpcode(empValue.getEmpcode());
emp.setDepartment(createDepartment("DEEE"));
emp.setEmpfname(empValue.getEmpfname());
emp.setEmplname(empValue.getEmplname());
emp.setEmpdob(empValue.getEmpdob());
emp.setEmpstatus(empValue.getEmpstatus());
emp.setEmptelno(empValue.getEmptelno());
emp.setAuditfield(empValue.getAuditfield());
session.update(emp);
}
} else
{
emp = new Employee();
emp.setEmpcode(empValue.getEmpcode());
emp.setDepartment(createDepartment("DEEE"));
emp.setEmpfname(empValue.getEmpfname());
emp.setEmplname(empValue.getEmplname());
emp.setEmpdob(empValue.getEmpdob());
emp.setEmpstatus(empValue.getEmpstatus());
emp.setEmptelno(empValue.getEmptelno());
emp.setAuditfield(empValue.getAuditfield());
session.save(emp);
}
tx.commit();
}
如您所见,该类已在适当的位置分配,我对异常感到困惑,期待一些帮助..
最佳答案
异常表示您没有为 Employee 类中标有 @id 的字段分配值。这个类看起来怎么样?您是要尝试通过提供的生成器之一生成 Id 值还是要手动设置它们?
关于java - hibernate |标识符生成异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3100950/