5 Replies Latest reply on Sep 15, 2006 4:30 AM by andydale

    ManyToMany problem

    andydale

      I am currently trying to implement a really simple many to many relationship using students and courses, but i cannot get it to work. The error always seems to be with the Collection in the none owning side of the relationship.

      Below are the exceptions i get:


      17:01:06,300 INFO [Ejb3Configuration] Processing PersistenceUnitInfo [
      name: test
      ...]
      17:01:06,302 INFO [Ejb3Configuration] found EJB3 Entity bean: test.Course
      17:01:06,307 INFO [Ejb3Configuration] found EJB3 Entity bean: test.Student
      17:01:06,307 WARN [Ejb3Configuration] Persistence provider caller does not implements the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
      17:01:06,309 INFO [Configuration] Reading mappings from resource: META-INF/orm.xml
      17:01:06,309 INFO [Ejb3Configuration] [PersistenceUnit: test] no META-INF/orm.xml found
      17:01:06,311 INFO [AnnotationBinder] Binding entity from annotated class: test.Course
      17:01:06,312 INFO [EntityBinder] Bind entity test.Course on table course
      17:01:06,312 WARN [AnnotationBinder] Hibernate does not support SequenceGenerator.initialValue()
      17:01:06,322 INFO [AnnotationBinder] Binding entity from annotated class: test.Student
      17:01:06,322 INFO [EntityBinder] Bind entity test.Student on table student
      17:01:06,323 WARN [AnnotationBinder] Hibernate does not support SequenceGenerator.initialValue()
      17:01:06,368 WARN [ServiceController] Problem starting service persistence.units:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,unitName=test
      org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns: [org.hibernate.mapping.Column(mCoursesEnrolledOn)]
      at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
      at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
      at org.hibernate.mapping.Property.isValid(Property.java:185)
      at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:395)
      at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
      at org.hibernate.cfg.Configuration.validate(Configuration.java:1021)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1206)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:631)
      at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:760)
      at org.hibernate.ejb.Ejb3Configuration.createContainerEntityManagerFactory(Ejb3Configuration.java:350)
      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(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at org.jboss.ejb3.ServiceDelegateWrapper.startService(ServiceDelegateWrapper.java:99)
      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:585)
      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.GeneratedMethodAccessor6.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      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 $Proxy120.start(Unknown Source)
      at org.jboss.ejb3.JmxKernelAbstraction.install(JmxKernelAbstraction.java:82)
      at org.jboss.ejb3.Ejb3Deployment.startPersistenceUnits(Ejb3Deployment.java:626)
      at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:475)
      at org.jboss.ejb3.Ejb3Module.startService(Ejb3Module.java:139)
      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:585)
      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.GeneratedMethodAccessor6.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      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 $Proxy41.start(Unknown Source)
      at org.jboss.ejb3.EJB3Deployer.start(EJB3Deployer.java:449)
      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:585)
      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.server.WebServiceDeployer.start(WebServiceDeployer.java:117)
      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 $Proxy42.start(Unknown Source)
      at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1007)
      at org.jboss.deployment.MainDeployer.start(MainDeployer.java:997)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:808)
      at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:771)
      at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      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 $Proxy6.deploy(Unknown Source)
      at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:421)
      at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:610)
      at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:263)
      at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.loop(AbstractDeploymentScanner.java:274)
      at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.run(AbstractDeploymentScanner.java:225)
      17:01:06,402 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,name=RelationTestBean,service=EJB3 with dependencies:
      17:01:06,402 INFO [JmxKernelAbstraction] persistence.units:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,unitName=test
      17:01:06,405 INFO [EJB3Deployer] Deployed: file:/usr/local/jboss-4.0.4.GA/server/default/tmp/deploy/tmp56627TestRelationships.ear-contents/TestRelationshipsEJB.jar
      17:01:06,407 INFO [EARDeployer] Started J2EE application: file:/usr/local/jboss-4.0.4.GA/server/default/deploy/TestRelationships.ear
      17:01:06,410 ERROR [URLDeploymentScanner] Incomplete Deployment listing:

      --- MBeans waiting for other MBeans ---
      ObjectName: persistence.units:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,unitName=test
      State: FAILED
      Reason: org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns: [org.hibernate.mapping.Column(mCoursesEnrolledOn)]
      I Depend On:
      jboss.jca:service=ManagedConnectionFactory,name=Postgres_Test
      Depends On Me:
      jboss.j2ee:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,name=RelationTestBean,service=EJB3

      ObjectName: jboss.j2ee:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,name=RelationTestBean,service=EJB3
      State: NOTYETINSTALLED
      I Depend On:
      persistence.units:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,unitName=test

      --- MBEANS THAT ARE THE ROOT CAUSE OF THE PROBLEM ---
      ObjectName: persistence.units:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,unitName=test
      State: FAILED
      Reason: org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns: [org.hibernate.mapping.Column(mCoursesEnrolledOn)]
      I Depend On:
      jboss.jca:service=ManagedConnectionFactory,name=Postgres_Test
      Depends On Me:
      jboss.j2ee:ear=TestRelationships.ear,jar=TestRelationshipsEJB.jar,name=RelationTestBean,service=EJB3



      Here is the relevant contents of Course.java
       /**
       * The list of student's enrolled on the course.
       */
       private Set < Student > mEnrolledStudents = new HashSet < Student >();
       .......
      
       /**
       * Return the students enrolled on the course.
       * @return mEnrolledStudents student's
       */
       @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
       @JoinTable(name = "course_student",
       joinColumns = {@JoinColumn(name = "course_id") },
       inverseJoinColumns = {@JoinColumn(name = "student_id") })
       public Set < Student > getStudents() {
       return mEnrolledStudents;
       }
      
      
       /**
       * Set the students enrolled on this course.
       * @param pEnrolledStudents students
       */
       public void setStudents(final Set < Student > pEnrolledStudents) {
       this.mEnrolledStudents = pEnrolledStudents;
       }
      


      And the relevant contents in Student.java
       /**
       * The list of the course the student is enrolled on.
       */
       private Collection < Course > mCoursesEnrolledOn = new ArrayList < Course >();
       ......
       /**
       * Return the courses the student is enrolled on.
       * @return mCoursesEnrolledOn courses
       */
       @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "mEnrolledStudents")
       public Collection < Course > getCourses() {
       return mCoursesEnrolledOn;
       }
      
      
       /**
       * Set the courses the student is enrolled on.
       * @param pCoursesEnrolledOn courses
       */
       public void setCourses(final Collection < Course > pCoursesEnrolledOn) {
       this.mCoursesEnrolledOn = pCoursesEnrolledOn;
       }
      


      I cannot understand what is wrong with my code as it is the same as all the EJB 3 examples i have seen, and read (O'Reilly Enterprise Java Beans 3.0). Do i have to decalre the the JoinTable in both Entities as I found an older example that said that (Jan 2005), but i think you don't need to now.

      Can anyone offer me any advice ?

      Thanks in advance,

      Andy

        • 1. Re: ManyToMany problem
          mwoelke

          Hi,
          First, you are not forced to use the JoinTable annotation. It works fine without (configure by exception - one goal of the ejb3 spec).

          I think the problem is what u specified with the mappedBy parameter of the manytomany. I think it should be 'students'.
          It should be the name of the property.

          Regards, Milan Wölke

          • 2. Re: ManyToMany problem
            andydale

            Hi,

            I figured out that the problem was with the mappedBy in the non owning side of the ManyToMany relationship. What i still do not completely understand is what it maps to, is mappedBy = "students" mapping to the java.util.Set variable ? it can't map to the name because it is not students so does it map to the type of it, or possibly to the getter and setters (get/setStudents) ?

            If anyone can clear this up for me, i would be thankful.

            Andy

            • 3. Re: ManyToMany problem
              scotttam

              As far as I understand the mappedBy, it is mapped to the getter name using bean rules on the owning side of the relationship.

              • 4. Re: ManyToMany problem
                dcbyers71

                Check your entity objects to determine where the @Id annotation is located. The examples that I have seen thus far have failed to highlight one sneaky piece of syntax for entities participating in relationships...the @Id element must modify the getXxx method for the ID field instead of modifying the entity field itself.

                If this is the issue, try relocating that @Id element and see if things get better.

                • 5. Re: ManyToMany problem
                  andydale

                  I have decided to always annotate the getters of each class variable as this seems to work, and in all the examples i have managed to find this seems to be the "done thing"