4 Replies Latest reply on Oct 15, 2007 1:21 PM by Tom Clifford

    List in EJB3 Entity:

    Adam Crowther Newbie

      Hi all,

      Please forgive this newbie post - I have searched the forum and the tutorials/specification for an answer without success. I'm sure the solution must be something simple.

      I'm trying to make an entity that has a collection of objects as one of it's attributes:

      package ch.adamcc.planeres2.entity;
      
      import java.util.ArrayList;
      import java.util.List;
      
      import javax.persistence.Entity;
      import javax.persistence.Id;
      
      @Entity
      public class SimpleEntity {
       private long _id;
       private String _stringField;
       private List<String> _stringList;
       private long[] _longArray;
      
       public SimpleEntity()
       {
       _stringList = new ArrayList<String>();
       _longArray = new long[2];
       }
      
       @Id
       public long getId() {
       return _id;
       }
      
       public void setId(long id) {
       _id = id;
       }
      
       public String getStringField() {
       return _stringField;
       }
      
       public void setStringField(String stringField) {
       _stringField = stringField;
       }
      
       public List<String> getStringList() {
       return _stringList;
       }
      
       public void setStringList(List<String> stringList) {
       _stringList = stringList;
       }
      
       public long[] getLongArray() {
       return _longArray;
       }
      
       public void setLongArray(long[] longArray) {
       _longArray = longArray;
       }
      }


      When I deploy the entity I get the following exception:
      2006-10-11 15:02:47,115 WARN [org.jboss.system.ServiceController] Problem starting service persistence.units:ear=planeres2.ear,jar=planeres2.jar,unitName=planeres2-persistence-unit
      javax.persistence.PersistenceException: org.hibernate.MappingException: Could not determine type for: java.util.List, for columns: [org.hibernate.mapping.Column(stringList)]
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:695)
       at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:127)
       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.ejb3.ServiceDelegateWrapper.startService(ServiceDelegateWrapper.java:102)
       at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
       at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
       at sun.reflect.GeneratedMethodAccessor63.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       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.GeneratedMethodAccessor5.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       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 $Proxy77.start(Unknown Source)
       at org.jboss.ejb3.JmxKernelAbstraction.install(JmxKernelAbstraction.java:96)
       at org.jboss.ejb3.Ejb3Deployment.startPersistenceUnits(Ejb3Deployment.java:467)
       at org.jboss.ejb3.Ejb3Deployment.start(Ejb3Deployment.java:317)
       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.GeneratedMethodAccessor63.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       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.GeneratedMethodAccessor5.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       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 $Proxy34.start(Unknown Source)
       at org.jboss.ejb3.EJB3Deployer.start(EJB3Deployer.java:449)
       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.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 $Proxy35.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.GeneratedMethodAccessor13.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       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:634)
       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)
      Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, for columns: [org.hibernate.mapping.Column(stringList)]
       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:410)
       at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
       at org.hibernate.cfg.Configuration.validate(Configuration.java:1026)
       at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
       at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:688)
       ... 99 more
      

      As far as I can tell I have followed the specification, but the documentation seems to be a little vague on how to do this and I couldn't find any examples to follow. I did try various variations on the same theme, without success.

      I would very much appreciate a hint on what I am doing wrong.

      Thanks very much in advance for your help.

      Yours,

      Adam.

        • 1. Re: List in EJB3 Entity:
          Chris Malan Novice

          Make both your List and long[] transient by annotating them as such. You will not be able to persist them in the database, but can use them inside your SimpleEntity class.

          Do not try to do too much in a persistence pojo. It is mainly, as the name says, an entity to persist.

          • 2. Re: List in EJB3 Entity:
            Andrew Dale Newbie

            Hi,

            If you want this list to be persistent, then annotate the List with @org.hibernate.annotations.CollectionOfElements

            Andy

            • 3. Re: List in EJB3 Entity:
              Adam Crowther Newbie

              Thank you both very much for taking the time to reply. :-)

              As you correctly said Andy, what I am looking for is to have a persistent attribute with multiple cardinality in my entity.

              I know I could simply define another entity and then use @OneToMany to define an association. In this case, however, the attribute is very simple (just a List of Strings or an array of longs), and it is only useful in the context of the parent entity - in short there's no real need to define a separate entity. That's why I thought to reduce the granularity of the entity interface by making it a simple attribute with multiple cardinality.

              I am a little surprised that the EJB3 specification doesn't support this requirement, and I had hoped to avoid using vendor specific extensions to the spec. It seems that's not feasable in this case, so I tried your solution and I'm glad to say it seems to work. :-)

              @CollectionOfElements
               public List<String> getStringList() {
               return _stringList;
               }
              
               public void setStringList(List<String> stringList) {
               _stringList = stringList;
               }
              
               @CollectionOfElements
               @IndexColumn(name="longArray_index")
               public long[] getLongArray() {
               return _longArray;
               }
              
               public void setLongArray(long[] longArray) {
               _longArray = longArray;
               }


              Now when I start the server, it deploys the entity and creates a set of 3 tables in the (mysql) database: simpleentity, simpleentity_longarray and simpleentity_stringlist. The strange thing is that I now get the following series of warnings during startup:

              13:22:57,651 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,651 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:57,666 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,666 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:57,713 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,713 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'
              13:22:57,713 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,713 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:57,713 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,713 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'
              13:22:57,713 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,713 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_stringlist'
              13:22:57,744 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,744 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:57,744 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,744 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'
              13:22:57,744 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,744 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_stringlist'
              13:22:57,869 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,869 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:57,869 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,869 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'
              13:22:57,869 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:57,869 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_stringlist'
              13:22:58,009 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,009 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:58,009 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,009 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'
              13:22:58,009 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,009 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_stringlist'
              13:22:58,087 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,087 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:58,087 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,087 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'
              13:22:58,087 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,087 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_stringlist'
              13:22:58,181 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,181 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
              13:22:58,181 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,181 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'
              13:22:58,181 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
              13:22:58,181 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_stringlist'
              

              As I mentioned above, I checked the database and it definitely does have the 3 listed tables in it.

              I'll dig deeper in to the hibernate documentation and let you know how I go.

              Thanks again very much for your help,

              Adam


              • 4. Re: List in EJB3 Entity:
                Tom Clifford Newbie

                Adam:

                I am trying to teach myself EJB3 using Bill Burke's book and JBoss 4.0.4GA, and I'm getting the same
                kinds of errors you indicated in your post on Jboss.org, at:
                http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3977853

                Did you ever get any kind of reply from JBoss (or anyone else) about the errors you got when
                deploying your EJB3 application:

                3:22:57,651 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
                13:22:57,651 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
                13:22:57,666 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
                13:22:57,666 WARN [JDBCExceptionReporter] Unknown table 'simpleentity'
                13:22:57,713 WARN [JDBCExceptionReporter] SQL Warning: 1051, SQLState: 42S02
                13:22:57,713 WARN [JDBCExceptionReporter] Unknown table 'simpleentity_longarray'

                Thanks......

                Tom C.
                Michigan State University
                East Lansing, Michigan USA
                cliffo60@ais.msu.edu
                http://www.msu.edu/user/cliffo60/
                http://www.clifforditservices.com/