8 Replies Latest reply on Feb 26, 2008 5:54 AM by taccart

    Unknown entity exception when Entities are in multiple jars

    benc

      I am trying to get an entity (C2) in one jar file (test1.jar) to reference an entity (C1) in a different jar file (test.jar) in the same ear as demonstrated in the code snippet below. As far as I can tell from the documentation and tutorials this separation into multiple jars should be not only possible but recommended practice.

      @Entity
      @Table(name = "c2")
      public class C2
      {
       protected C1 c1;
      
       @ManyToOne(optional=false)
       @JoinColumn(name="c1")
       public C1 getC1()
       {
       return c1;
       }
      
       public void setC1(C1 c1)
       {
       this.c1 = c1;
       }
      
      ...
      }
      
      


      However, when I deploy the ear I get the following unknown entity exception.


      11:20:28,446 WARN [ServiceController] Problem starting service persistence.units:ear=test.ear,unitName=test2
      org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test2.C2.c1 references an unknown entity: test.C1
      at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56)
      at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
      at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
      at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
      at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233)
      at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:869)
      at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:407)
      at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126)
      at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:246)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.ejb3.ServiceDelegateWrapper.startService(ServiceDelegateWrapper.java:103)
      at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
      at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
      at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
      at $Proxy0.start(Unknown Source)
      at org.jboss.system.ServiceController.start(ServiceController.java:417)
      at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
      at $Proxy59.start(Unknown Source)
      at org.jboss.ejb3.JmxKernelAbstraction.install(JmxKernelAbstraction.java:120)
      at org.jboss.ejb3.Ejb3Deployment.startPersistenceUnits(Ejb3Deployment.java:627)
      at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:351)
      at org.jboss.ejb3.Ejb3Module.startService(Ejb3Module.java:91)
      at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
      at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
      at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
      at $Proxy0.start(Unknown Source)
      at org.jboss.system.ServiceController.start(ServiceController.java:417)
      at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
      at $Proxy33.start(Unknown Source)
      at org.jboss.ejb3.EJB3Deployer.start(EJB3Deployer.java:512)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
      at org.jboss.mx.interceptor.DynamicInterceptor.invoke(DynamicInterceptor.java:97)
      at org.jboss.system.InterceptorServiceMBeanSupport.invokeNext(InterceptorServiceMBeanSupport.java:238)
      at org.jboss.ws.integration.jboss42.DeployerInterceptor.start(DeployerInterceptor.java:93)
      at org.jboss.deployment.SubDeployerInterceptorSupport$XMBeanInterceptor.start(SubDeployerInterceptorSupport.java:188)
      at org.jboss.deployment.SubDeployerInterceptor.invoke(SubDeployerInterceptor.java:95)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
      at $Proxy34.start(Unknown Source)
      at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1025)
      at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1015)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:819)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:782)
      at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
      at $Proxy9.deploy(Unknown Source)
      at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:421)
      at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:634)
      at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:263)
      at org.jboss.deployment.scanner.AbstractDeploymentScanner.startService(AbstractDeploymentScanner.java:336)
      at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
      at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
      at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
      at $Proxy0.start(Unknown Source)
      at org.jboss.system.ServiceController.start(ServiceController.java:417)
      at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
      at $Proxy4.start(Unknown Source)
      at org.jboss.deployment.SARDeployer.start(SARDeployer.java:302)
      at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1025)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:819)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:782)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:766)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
      at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
      at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
      at $Proxy5.deploy(Unknown Source)
      at org.jboss.system.server.ServerImpl.doStart(ServerImpl.java:482)
      at org.jboss.system.server.ServerImpl.start(ServerImpl.java:362)
      at org.jboss.Main.boot(Main.java:200)
      at org.jboss.Main$1.run(Main.java:508)
      at java.lang.Thread.run(Thread.java:619)
      11:20:28,532 INFO [EJB3Deployer] Deployed: file:/home/benc/cf/3.0/jboss-4.2.1.GA/server/test/deploy/test.ear/test2.jar/
      11:20:28,620 INFO [EARDeployer] Started J2EE application: file:/home/benc/cf/3.0/jboss-4.2.1.GA/server/test/deploy/test.ear/
      11:20:28,624 ERROR [URLDeploymentScanner] Incomplete Deployment listing:

      --- MBeans waiting for other MBeans ---
      ObjectName: persistence.units:ear=test.ear,unitName=test2
      State: FAILED
      Reason: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test2.C2.c1 references an unknown entity: test.C1
      I Depend On:
      jboss.jca:service=DataSourceBinding,name=FCFDS

      --- MBEANS THAT ARE THE ROOT CAUSE OF THE PROBLEM ---
      ObjectName: persistence.units:ear=test.ear,unitName=test2
      State: FAILED
      Reason: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test2.C2.c1 references an unknown entity: test.C1
      I Depend On:
      jboss.jca:service=DataSourceBinding,name=FCFDS



      Everything works as expected with the two files are in the same jar, it only fails when I put the class files in separate jars.

      Complete source code and ear file can be found at http://www.noverant.com/download/ejb3jars.zip

      Thank you in advance for your help.


        • 1. Re: Unknown entity exception when Entities are in multiple j
          alrubinger

          Well, you've got an association between tables that are managed by two separate Persistence Units...

          What documentation directed you to take this approach?

          To get classes from different JARs in the same persistence context, use the "jar-file" or "class" nodes under the "persistence-unit" root in persistence.xml.

          I'm not sure if this bug has been fixed (haven't myself upgraded yet), but there was a problem as recent as JBoss 4.0.5 where JARs referenced by the "jar-file" element were getting (incorrectly) scanned relative to $JBOSS_HOME/bin. I currently use the "class" element to incorporate classes from other JARs into my EMs and all works fine.

          S,
          ALR

          • 2. Re: Unknown entity exception when Entities are in multiple j
            benc

            Thanks ALR,

            The class entry was what I was missing. I had tried the jar-file entry and JBoss couldn't find it. It doesn't seem to going relative to bin in 4.2.1; but, I can't make it construct the correct path.

            This is my first foray into EJB3 land. I've been reading JBoss docs, trail blazer demos, these forums, Sun's EJB3 specs, ServerSide articles, and most other things I can find. :) Although every example I found has had everything in one jar/persistence context...

            Any suggestions on EJB 3 best practices and design patterns would be appreciated. :)

            Ben

            • 3. Re: Unknown entity exception when Entities are in multiple j
              alrubinger

              Glad that worked for you. :)

              Looks like you're reading all the right stuff, including this forum. But keep in mind there's no one correct way of doing things, and often your simplest "practice" or "design pattern" is the best choice.

              I promise that's not a cop-out answer.

              S,
              ALR

              • 4. Re: Unknown entity exception when Entities are in multiple j
                alrubinger

                ...allright fine, the big secret is:

                Use the Adaptor Pattern with a Singleton obtained through a Factory, and EJB3 will work for you.

                S,
                ALR

                • 5. Re: Unknown entity exception when Entities are in multiple j
                  slotos

                  I have the same problem using 2 jars in an ear file. But neither the class nor the jar-file tag in a persistence.xml of the META-INF directory of the ear file works.

                  The aim is to have a basic.jar where general entity classes exist. In the concrete.jar I would like to have derived entity classes from the basic.jar. Both should use the same persistence unit.

                  I use JBoss 4.2.0 GA.

                  It would be nice if benc could send his readjusted ejb3jar project to me.

                  Thanks,

                  Thomas

                  • 6. Re: Unknown entity exception when Entities are in multiple j
                    benc

                    Sorry for the slow reply, I was out sick all of last week.

                    The only change that has been made to the posted ear to make it work for me is adding the class line to test2.jar/META-INF/persistence.xml

                    <persistence>
                     <persistence-unit name="test2">
                     <jta-data-source>java:/FCFDS</jta-data-source>
                     <class>test.C1</class>
                     <properties>
                     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                     <property name="hibernate.hbm2ddl.auto" value="create"/>
                     <property name="hibernate.show_sql" value="true"/>
                     <property name="hibernate.use_outer_join" value="true"/>
                     <property name="hibernate.jdbc.batch_size" value="100"/>
                     </properties>
                     </persistence-unit>
                    </persistence>
                    


                    To the best of my knowledge each jar file has to have it's own persistence unit defined in the persistence.xml. The class and in theory the jar-file tags essentially import the defined classes into the current jar's persistence unit.

                    • 7. Re: Unknown entity exception when Entities are in multiple j
                      alrubinger

                      Slotos had sent me his EAR, and I had this to comment, in case this might help future readers:

                      At quick glance, you should definitiely not have a persistence.xml in the META-INF of the EAR. If you'd like to keep a JAR of entities but not mark this JAR for its own persistence unit, skip the persistence.xml in that JAR, include it as a "java" module in application.xml of the EAR, and reference the classes from it that you need in the persistence.xml of the *other* JAR, which should be "ejb" in the EAR's application.xml.

                      ...that's a long run-on sentence, but y'all should get the gist.

                      S,
                      ALR

                      • 8. Re: Unknown entity exception when Entities are in multiple j
                        taccart


                        Maybe adding the external C1-ejb.jar in persistence.xml of C2-ejb.jar
                        is sufficient ?

                        in C2-ejb.jar /META-INF/persistence.xml you'd have something like

                        <persistence-unit name="yourC2stuff">
                        <jta-data-source>java:/yourC2DSstuff</jta-data-source>
                        <jar-file>../c1-ejb.jar</jar-file>
                        ...