2010-06-23 49 views
2

我得到一个异常时,我试图通过Hibernate持久层来保存一些数据,例外的是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(); 
    } 

,你可以看到在类内得到适当的地方分配的,我很困惑例外情况,期待一些帮助..

回答

2

异常消息ids for this class must be manually assigned before calling save()是不言自明的。

发生这种情况是因为您使用的是assigned内置发生器。 assigned生成器显式地告诉Hibernate应用程序将分配标识符。从部分5.1.4.1. Generator

允许应用程序在调用save()之前为该对象分配标识符。如果没有指定<generator>元素,这是默认策略。

如果这不是你想要的,使用其他发电机,例如native(这 选择identitysequencehilo根据底层数据库的能力):

<class name="LineItem" table="`Line Item`"> 
    <id name="id" column="`Item Id`"/> 
    <generator class="native"/> 
    </id> 
    ... 
</class> 
3

该异常说明您没有将值分配给Employee类中用@id标记的字段。这堂课怎么样?你是否试图通过提供的生成器生成Id值,或者你想手动设置它们吗?

+0

设置为类.hbm文件 – Switch 2010-06-23 11:14:48

+0

@MMRUser你能澄清哪个字段被标记为在映射的“ID” =“分配”? – 2010-06-23 11:52:38

+0

empCode被标记为ID – Switch 2010-06-23 12:37:58

0

使用以下类型的代码来保存员工 如果这没有帮助,请粘贴映射文件以了解字段empCode的详细信息。

helper = new HibernateHelper(); 
Customer custObject; 
if (customer.getId() == 0) { 
       custObject = new Customer(); 
       custObject.setCreatetimestamp(new Timestamp(System.currentTimeMillis())); 
      } else { 
       custObject = (Customer) helper.getSession().load(Customer.class, customer.getId()); 
      } 
      custObject.setName(customer.getName()); 
      custObject.setAddress(customer.getAddress()); 
      custObject.setBillToAddress(customer.getBillToAddress()); 
      custObject.setBillToPerson(customer.getBillToPerson()); 
      custObject.setUpdatetimestamp(new Timestamp(System.currentTimeMillis())); 
      custObject.setEmail(customer.getEmail()); 
      custObject.setDeleted(false); 
      Currency curr = (Currency) helper.getSession().load(Currency.class, x); 
      custObject.setCurrencyId(curr); 

      custId = (Long) helper.getSession().save(custObject); 
      helper.commit();