1 Reply Latest reply on May 7, 2008 11:47 AM by Derick

    Having trouble with OneToOne with PrimaryKeyJoinColumn

    Lori Debetaz Newbie

      When I try to model a bidirectional OneToOne mapping using PrimaryKeyJoinColumn I receive an "unknown mappedBy" error. When I try to model it as a unidirectional OneToOne mapping with PrimaryKeyJoinColumn I don't receive this error (there's no mappedBy so that's not a surprise) but there is no foreign key relationship created in the database.

      I'm using the hibernate-all-alpha8.jar (at least that's the name of the jar picked up from taylor.sourceforge.net/maven2 with the jboss-ejb3-all-alpha8.jar) and embedded jboss.

      I have a Party (parent) and Person (child) which share a primary key. The party table needs to control the creation of the primary key because not all Party records have a corresponding Person record but every Person is a Party.

      Party code:

      @Entity
      public class Party implements Serializable {
      
       @Id
       @GeneratedValue(strategy=javax.persistence.GenerationType.AUTO)
       public Long getId() {
       return id;
       }
      
       @OneToOne(optional=true, fetch=javax.persistence.FetchType.LAZY, mappedBy="party", cascade=javax.persistence.CascadeType.ALL)
       public Person getPerson() {
       return person;
       }
      


      Person code
      @Entity
      public class Person implements Serializable {
       @Id
       public Long getId() {
       return id;
      
       @OneToOne(fetch=javax.persistence.FetchType.EAGER,cascade=javax.persistence.CascadeType.ALL)
       @PrimaryKeyJoinColumn
       public Party getParty() {
       return party;
       }
      


      Error:
      
      DEBUG 14-09 10:57:50,671 (PropertyBinder.java:make:122) -Building property person
      DEBUG 14-09 10:57:50,671 (PropertyBinder.java:make:157) -Cascading person with all
      ERROR 14-09 10:57:50,687 (AbstractController.java:incrementState:350) -Error installing to Start: n
      ame=persistence.units:jar=classes.jar,unitName=try_talyor_prj state=Create
      org.hibernate.AnnotationException: Unknown mappedBy in: com.ndo.healthcare.entities.Party.person, re
      ferenced property unknown: com.ndo.healthcare.entities.Person.party
       at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:127)
       at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1049)
       at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:302)
       at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1034)
       at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1015)
       at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:751)
       at org.hibernate.ejb.Ejb3Configuration.createContainerEntityManagerFactory(Ejb3Configuration.java:3
      50)
       at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.
      java:119)
       at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:264)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at org.jboss.reflect.plugins.introspection.ReflectionUtils.invoke(ReflectionUtils.java:55)
       at org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl.invoke(ReflectMethodInfoImpl.java:
      107)
       at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.dispatch(BasicMethodJoinPoint.java:66)
       at org.jboss.kernel.plugins.dependency.KernelControllerContextActions.dispatchJoinPoint(KernelContr
      ollerContextActions.java:100)
       at org.jboss.kernel.plugins.dependency.KernelControllerContextActions$LifecycleAction.installAction
      (KernelControllerContextActions.java:582)
       at org.jboss.kernel.plugins.dependency.KernelControllerContextActions$KernelControllerContextAction
      .install(KernelControllerContextActions.java:175)
       at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextA
      ctions.java:51)
       at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:22
      6)
       at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:593)
       at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:346)
       at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:438)
       at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:379)
       at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:225)
       at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:151)
       at org.jboss.kernel.plugins.dependency.AbstractKernelController.install(AbstractKernelController.ja
      va:79)
       at org.jboss.kernel.plugins.dependency.AbstractKernelController.install(AbstractKernelController.ja
      va:73)
       at org.jboss.ejb3.MCKernelAbstraction.install(MCKernelAbstraction.java:91)
       at org.jboss.ejb3.Ejb3Deployment.startPersistenceUnits(Ejb3Deployment.java:626)
       at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:475)
       at org.jboss.ejb3.embedded.EJB3StandaloneDeployer.start(EJB3StandaloneDeployer.java:460)
       at org.jboss.ejb3.embedded.EJB3StandaloneBootstrap.scanClasspath(EJB3StandaloneBootstrap.java:291)
       at net.taylor.jboss.EJB3Container.startupEmbeddedJboss(EJB3Container.java:63)
       at net.taylor.jboss.EJB3Container.setUp(EJB3Container.java:31)
       at junit.extensions.TestSetup$1.protect(TestSetup.java:18)
       at junit.framework.TestResult.runProtected(TestResult.java:124)
       at junit.extensions.TestSetup.run(TestSetup.java:23)
       at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:12
      8)
       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)
      
      


      When I comment out the OneToOne annotation in Party, then there are no errors reported, but there is no foreign key created in the Person table to refer to the Party table. I'm assuming there should be.

      So,
      (1) Am I modeling something wrong in the bidirectional OneToOne case? I do realize that I need to either use a GenericGenerator in the Person class to get the primary key value in the Party table or explicitly set it, but this "unknown mappedBy" error is my concern.

      (2) In the unidirectional OneToOne case, am I correct to assume that a foreign key relationship should be created?

      Thanks for any help.

      Lori Debetaz