-->
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: inheritance: returning mapped super from child getter
PostPosted: Thu Aug 24, 2006 8:29 am 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
Hi,

I have 2 class hierarchies like this:
Definition
--Factor
--Status
Item
--FactorItem
--StatusItem

Definition and Item are abstract mapped classes and all of the child classes are mapped to their own tables. The class Definition contains a collection of Item and Item contains a reference to its Definition. What this means is, a Factor contains FactorItems and Status contains StatusItems.

I'm having a problem with the placement of the many-to-one/one-to-many relations. I tried putting Definition in Item and I provided a getter with the return type Definition. I had an exception with Hibernate, so I tried making the getter in Item abstract and I implement the getter in FactorItem and StatusItem, but the return type is still Definition. I still get the Hibernate exception.

I don't really know what the problem is. Perhaps I'm implementing my inheritance incorrectly? Can anyone help?

Hibernate version:
3.2

Mapping documents:

@MappedSuperclass
public abstract class Definition extends BaseObject {

private List<Item> items;

protected Definition() {}

@OneToMany(mappedBy="definition")
public List<Item> getItems() {
return items;
}
}

@Entity
public class Factor extends Definition {

protected Factor() {}

}

@Entity
public class Status extends Definition {

protected Status() {}

}

@MappedSuperclass
public abstract class Item extends BaseObject {

protected Item() {}

public abstract Definition getDefinition();

}

@Entity
public class StatusItem extends Item {

private Status definition;

protected StatusItem() {}

@Override
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
public Definition getDefinition() {
return definition;
}

void setDefinition(Status definition) {
this.definition = definition;
}
}
@Entity
public class FactorItem extends Item {
private Factor definition;

private FactorItem() {}

@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
public Definition getDefinition() {
return definition;
}

void setDefinition(Factor definition) {
this.definition = definition;
}

}

And this is the spring/hibernate configuration file:

<beans>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>ce.bo.BaseObject</value>
<value>ce.bo.Definition</value>
<value>ce.bo.Factor</value>
<value>ce.bo.Item</value>
<value>ce.bo.FactorItem</value>
</list>
</property>

</bean>

<bean id="FactorDao" class="ce.dao.hibernate.CEDaoHibernate" abstract="false" singleton="true">
<constructor-arg>
<value>ce.bo.Factor</value>
</constructor-arg>
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<bean id="FactorItemDao" class="ce.dao.hibernate.CEDaoHibernate" abstract="false" singleton="true">
<constructor-arg>
<value>ce.bo.FactorItem</value>
</constructor-arg>
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>

Full stack trace of any exception that occurs:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CETestDBTarget' defined in class path resource [applicationContextTestDB.xml]: Cannot resolve reference to bean 'FactorDao' while setting bean property 'FactorDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FactorDao' defined in class path resource [hibernateObjects.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [hibernateObjects.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on ce.bo.FactorItem.definition references an unknown entity: ce.bo.Definition
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'FactorDao' defined in class path resource [hibernateObjects.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [hibernateObjects.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on ce.bo.FactorItem.definition references an unknown entity: ce.bo.Definition
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [hibernateObjects.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on ce.bo.FactorItem.definition references an unknown entity: ce.bo.Definition
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on ce.bo.FactorItem.definition references an unknown entity: ce.bo.Definition
at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:40)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:288)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1205)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:871)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:797)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:901)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:870)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:393)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:257)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:226)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:115)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:798)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:589)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:389)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:257)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:226)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:115)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:798)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:589)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:389)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:257)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:254)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:332)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
at org.springframework.test.AbstractSpringContextTests.loadContextLocations(AbstractSpringContextTests.java:130)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.loadContextLocations(AbstractDependencyInjectionSpringContextTests.java:224)
at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:110)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:192)
at junit.framework.TestCase.runBare(TestCase.java:128)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:228)
at junit.framework.TestSuite.run(TestSuite.java:223)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 11:46 am 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
I read the inheritance/polymorphism chapter a bit more thoroughly and I realize that what I'm trying to do is probably not possible. I don't have table for the superclasses, so it looks like I would use the implicit polymorphism, but I wanted to use the generated Id. I will have to consider if I want to create a table for the superclass which only contains the id.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.