-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA annotation - ManyToOne on a MappedSuperclass
PostPosted: Wed Jul 11, 2007 9:43 am 
Newbie

Joined: Fri Sep 03, 2004 2:20 am
Posts: 7
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


Top
 Profile  
 
 Post subject: Problem only occurs during update, insert works
PostPosted: Wed Jul 11, 2007 9:48 am 
Newbie

Joined: Fri Sep 03, 2004 2:20 am
Posts: 7
If it wasn't clear, there are no problems when saving - the Option instance is persisted with the correct foreign key populated pointing to the right OptionQuestion.

This problem is occurring strictly on a retrieve for a record saved in a different session.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.