views:

40

answers:

1

I have the following method in my Java application's DAO layer:

public void save(Employee emp) {
     System.out.println("emp type: " + emp.getClass().getName);
     getHibernateTemplate().save(emp);
     System.out.println("object saved!");
     System.out.flush();
}

The employee class does not extend from any other classes and has the following hbm file:

<hibernate-mapping>    
 <class name="org.myCompany.Employee" table="employee">
 <!-- fields omitted to save space -->
</hibernate-mapping>

Yet the insert fails with a java.lang.ClassCastException. At first, I thought something was wrong with my mapping (like an Integer mapping to a boolean) but then I turned on hibernate's show_sql debugging and found the following in my log file:

emp type: org.myCompany.Employee
Hibernate: insert into customer (.......) values (......)
java.lang.ClassCastException

Why did it pick an entirely random table to insert into? I'm sure that I must have some config file mis-configured, but I don't know which one. I checked the following:

  • applicationContext-hibernate.xml -> both the Customer and Employee objects are mapped to their correct hbm files
  • neither Customer or Employee have an inheritance relationship (there isn't even a common parent class like Person or User)

What else could I try?

A: 

In the calling class, I have the following two calls:

UserDAO.saveCustomer(customer);
UserDAO.saveEmployee(employee);

I thought that the customer object was saving correctly. I even put a debugging statement between the two calls and the debug method printed. However, commenting out the UserDAO.saveCustomer line fixed the problem. This made me examine the customer object more closely and I found that it was corrupt (wrong class). Hibernate must have been caching the insert statement until the Transaction was complete.

So this issue is now resolved.

David
You should edit your question instead of posting an answer, generally
Steven Schlansker