休眠| 标识符生成异常

Swi*_*tch 2 java orm 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();
    }
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,该类被分配到适当的位置,我对异常感到困惑,期待一些帮助..

ber*_*ert 5

该异常表示您没有为 Employee 类中标有 @id 的字段分配任何值。这堂课怎么样?您是否尝试通过提供的生成器之一生成 Id 值,或者您想手动设置它们?