-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with @OneToOne annotation
PostPosted: Fri Feb 27, 2009 3:27 pm 
Newbie

Joined: Wed Jan 10, 2007 3:07 pm
Posts: 8
Hibernate version:
3.2.6.ga
annotations - 3.2.1.ga

I'm pretty new to annotations, and I'm trying to get a one-to-one relationship working with no luck. The situation is that I have a class (Company) that extends a MappedSuperClass (PersistentEntity). Company (and some other PersistentEntity subclasses contains an Address. I've searched the web and this is what I've got at this point. From my experience with mapping files it *seems* reasonable.

Code:
@MappedSuperclass
public abstract class PersistentEntity
{
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(length=32)
    protected String oid;
...


@Entity
@Table(name="company")
public class Company extends PersistentEntity
{
    @OneToOne(optional=true, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @PrimaryKeyJoinColumn(name="oid",  referencedColumnName="addr_for")
    private PostalAddress address;
...


@Entity
@Table(name="address")
public class PostalAddress
{
    @Id
    @GeneratedValue(generator="foreign")   
    @GenericGenerator(name="foreign", strategy = "foreign",
                      parameters={@Parameter(name="property", value="entity")})   
    @Column(name="addr_for", length=32)
    private String ownedBy;
    @OneToOne(optional=false, mappedBy="oid")
    private PersistentEntity entity;
...



I'm not thrilled about having a PersistentEntity in PostalAddress, but I can live with that. I can't live with the exception that I get with this (Unknown mappedBy in: com.efx.gds.model.PostalAddress.entity, referenced property unknown: com.efx.gds.model.PersistentEntity.oid). All three classes are listed in the configuration as annotated classes. Why is PersistentEntity.oid unknown (the package is correct)? Does it have anything to do with PersistentEntity being a MappedSuperClass rather than an Entity? How can I resolve this?

Full stack trace of any exception that occurs:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/efx/gds/dao/hibernate/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: com.efx.gds.model.PostalAddress.entity, referenced property unknown: com.efx.gds.model.PersistentEntity.oid
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
at org.springframework.test.AbstractSingleSpringContextTests.createApplicationContext(AbstractSingleSpringContextTests.java:199)
at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:179)
at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:158)
at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:105)
at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:87)
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:45)
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)
Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: com.efx.gds.model.PostalAddress.entity, referenced property unknown: com.efx.gds.model.PersistentEntity.oid
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:129)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1136)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:673)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
... 32 more


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 5:27 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
mappedBy relates to the property-name on the other side, which has to be the type of the mapped entity. In your example oid is a string, but mappedBy expects an attribute of type address.

Move your field "address" from Company to PersistentEntity if you want to be able to relate to that in your address.

An even nicer solution to map addresses is to use @Embeddable, as described in the reference guide.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 12:13 pm 
Newbie

Joined: Wed Jan 10, 2007 3:07 pm
Posts: 8
mmerder wrote:
mappedBy relates to the property-name on the other side, which has to be the type of the mapped entity. In your example oid is a string, but mappedBy expects an attribute of type address.

Move your field "address" from Company to PersistentEntity if you want to be able to relate to that in your address.


OK, I think I see what you're saying. But not all PersistentEntity's will have addresses. I guess I could create an intermediate class (PersistentEntityWithAddress), but that feels like a hack.

mmerder wrote:
An even nicer solution to map addresses is to use @Embeddable, as described in the reference guide.


I disagree that this is a nicer solution. It's what I have done for the time being, and it works. But if I understand Embedable correctly then the columns for address have to be in the Company table. And they will also have to be added to the tables for all other PersistentEntity subclasses that have an address. I would much prefer to have a separate table for addresses, especially since that will make things much easier if in the future it's decided that we need to support multiple addresses (mailing, billing, shipping, etc). Of course, in that case it would become a OneToMany relationship.

Thanks for your response, I'll continue to play with it some.

Dave


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 12:52 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
dderry wrote:
OK, I think I see what you're saying. But not all PersistentEntity's will have addresses. I guess I could create an intermediate class (PersistentEntityWithAddress), but that feels like a hack.

Well, I don't think its a hack at all. The mapping you have right now is not really clear. You say an address can relate to any PersistentEntity but not every PersistentEntity has an address. That's not logical. So introducing an "intermediate" class is the perfect abstraction for such a case. Another solution would be to make all your entities having an address implement an interface Addressable.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.