views:

39

answers:

3

I'm getting an exception when I try to save some data through hibernate persistence layer, the exception is

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();
    }

as you can see the class get assigned within appropriate places, I'm confused about the exception, expecting some help..

+3  A: 

The Exception says that you have no value assigned to field marked with @id in the Employee class. How that this class look like? Are you trying to generate the Id values through one of the provided generators or would you like to set them manually?

bert
it is set to class="assigned" in .hbm file
MMRUser
@MMRUser can you clarify which field is marked as the "id" in your mapping?
matt b
empCode is marked as ID
MMRUser
A: 

The exception message ids for this class must be manually assigned before calling save() is self explanatory.

This happens because you are using the assigned built-in generator. The assigned generator explicitly tells Hibernate that the application is going to assign the identifier. From the section 5.1.4.1. Generator:

lets the application assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.

If this is not what you want, use another generator, for example native (this selects identity, sequence or hilo depending upon the capabilities of the underlying database):

<class name="LineItem" table="`Line Item`">
  <id name="id" column="`Item Id`"/>
    <generator class="native"/>
  </id>
  ...
</class>
Pascal Thivent
A: 

Use following kind of code to save employee if this is not helpful please paste mapping file to know details of field 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();
Sonal Patil