I have a OneToOne relationship between two entities, but the
foreign key does not reference the primary key, instead it reference another unique key in the referenced table. In such a case, I am getting
java.lang.ClassCastException: IncidentStatus
while trying to execute a simple find().
Hibernate version: Hibernate EntityManager 3.2.0.CR1, Hibernate Annotations 3.2.0.CR1, Hibernate 3.2 cr3
Mapping documents:
Code:
@Entity public class Incident {
@Id String id;
// See FK does *not* reference PK of IncidentStatus table
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(referencedColumnName="code")
IncidentStatus incidentStatus;
Incident(){}
Incident(String id) {
this.id = id;
}
}
@Entity public class IncidentStatus {
@Id String id;
@Column(nullable=false)
String code;
@OneToOne(mappedBy="incidentStatus")
Incident incident;
public IncidentStatus() {}
public IncidentStatus(String id) {
this.id = id;
this.code = "code_" + id;
}
}
Full stack trace of any exception that occurs:
Exception in thread "main" java.lang.IllegalArgumentException: IncidentStatus
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:190)
at Client.main(Client.java:11)
Caused by: java.lang.ClassCastException: IncidentStatus
at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:103)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:1995)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1371)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1299)
at org.hibernate.loader.Loader.getRow(Loader.java:1197)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:568)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1784)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2861)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:393)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:374)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:137)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:193)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:101)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:173)
... 1 more
Name and version of the database you are using: Apache Derby, version: 10.1.1.0
The generated SQL (show_sql=true):
Hibernate: select incident0_.id as id0_1_, incident0_.incidentStatus_code as incident2_0_1_, incidentst1_.id as id1_0_, incidentst1_.code as code1_0_ from Incident incident0_ left outer join IncidentStatus incidentst1_ on incident0_.incidentStatus_code=incidentst1_.code where incident0_.id=?
Debug level Hibernate log excerpt: