2010-06-23 55 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();