Hi,
I hope someone can help me as I'm not a JPA guru. The problem I'm having deals with having a ManyToOne mapping to an abstract class and Hibernate is throwing an ObjectNotFoundException when populating the aggregated object.
I'm currently running the following versions:
- hibernate-3.2.2.ga
- hibernate-annotations-3.2.1.ga
- Spring 2.1-m2.
My class hierarchy is:
@Entity
@Table(name = "ELEMENT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Element extends AbstractSortableModel {
}
@Entity
@MappedSuperclass
public abstract class Question extends Element {
...
}
@Entity
@MappedSuperclass
public class OptionQuestion extends Question {
...
}
@Entity
@DiscriminatorValue(value = "SOQ")
public class SingleOptionQuestion extends OptionQuestion {
...
}
@Entity
@DiscriminatorValue(value = "MOQ")
public class MultiOptionQuestion extends OptionQuestion {
...
}
I have a class called Option which looks like:
@Entity
@Table(name = "OPTION_VALUE")
public class Option extends AbstractSortableModel {
@ManyToOne private OptionQuestion question;
}
When I try to inflate an instance of Option, Hibernate is complaining that it can't find the associated OptionQuestion (ObjectNotFoundException).
If I were to map Option as
@Entity
@Table(name = "OPTION_VALUE")
public class Option extends AbstractSortableModel {
@ManyToOne(targetEntity = SingleOptionQuestion.class) private OptionQuestion question;
}
the query works!
My question is how do I map an aggregation to a class that is not on the leaf of the hierarchy? Have I missed an annotation? Or is this a Hibernate bug?
Note that this error only occurs when accessing a record which was saved in a different session. If I were to save and retrieve in the same session, the error does not occur.
The stack trace:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.company.scrap.model.element.OptionQuestion#5]
at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:145)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:846)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:557)
at org.hibernate.type.EntityType.resolve(EntityType.java:379)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
Can anyone out there help?
Many thanks,
dave
|