It seems that Hibernate is unable to load two objects with the same base class (using the table per subclass strategy) at once. I have the following structure:
Code:
@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable {
private String id;
private String firstname;
private String lastname;
private char gender;
private String address;
private String phone;
// other properties plus getters and setters
}
@Entity
@Table(name = "customer")
@PrimaryKeyJoinColumn(name="customer_id")
public class Customer extends Person {
private String workPlace;
private String workPhone;
// other properties plus getters and setters
}
@Entity
@Table(name="employee")
@PrimaryKeyJoinColumn(name="employee_id")
public class Employee extends Person {
private String position;
private String username;
private String password;
// other properties plus getters and setters
}
@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "invoice")
public class Invoice implements Serializable {
private InvoiceID id;
private Customer customer;
private Employee salesperson;
private Date date;
// other properties plus getters and setters
}
Everything works okay until we encounter an invoice where a customer is also an employee. So you have data in the employee and customer table all linked with the same id to the parent table.
The following is the error message that appeares when loading the invoice:
Code:
19:31:40,157 DEBUG SQL:111 - /* load hakim.model.Invoice */ select invoice0_.invoice_id ...... left outer join declaration declaratio5_ on invoice0_.invoice_id=declaratio5_.invoice_id and invoice0_.shop_id=declaratio5_.shop_id where invoice0_.invoice_id=? and invoice0_.shop_id=?
19:31:40,262 INFO DefaultLoadEventListener:134 - Error performing load command
org.hibernate.WrongClassException: Object with id: 1975121701 was not of the specified subclass: hakim.model.Employee (loaded object was of wrong class class hakim.model.Customer)
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1267)
at org.hibernate.loader.Loader.getRow(Loader.java:1219)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
at org.hibernate.loader.Loader.doQuery(Loader.java:724)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
at org.springframework.orm.hibernate3.HibernateTemplate$1.doInHibernate(HibernateTemplate.java:528)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
.....
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
19:31:40,299 ERROR InvoiceController:70 - Error while finding invoices: org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: Object with id: 1975121701 was not of the specified subclass: hakim.model.Employee (loaded object was of wrong class class hakim.model.Customer); nested exception is org.hibernate.WrongClassException: Object with id: 1975121701 was not of the specified subclass: hakim.model.Employee (loaded object was of wrong class class hakim.model.Customer)
It seams that Hibernate doesn't know how to handle a situation where a customer and employee object refers to the same person. Everything works perfectly when they are different.
Could someone please advice me how to solve this?