5 Replies Latest reply on Sep 12, 2005 7:57 AM by rsuffling

    Null Primary Key

    spiffer

      I am using MySQL to store an EJB and after inserting a new instance of the class into the database I get the following:

      javax.ejb.CreateException: Primary key for created instance is null.
      org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:574)
      org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:222)
      org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConnectionInterceptor.java:266)
      org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:766)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:585)
      org.jboss.invocation.Invocation.performCall(Invocation.java:345)
      org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityContainer.java:1113)
      org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.java:90)
      org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invokeHome(EntitySynchronizationInterceptor.java:192)
      org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invokeHome(CachedConnectionInterceptor.java:212)
      org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.java:90)
      org.jboss.ejb.plugins.EntityInstanceInterceptor.invokeHome(EntityInstanceInterceptor.java:117)
      org.jboss.ejb.plugins.EntityLockInterceptor.invokeHome(EntityLockInterceptor.java:61)
      org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCreationInterceptor.java:28)
      org.jboss.ejb.plugins.CallValidationInterceptor.invokeHome(CallValidationInterceptor.java:41)
      org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:109)
      org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:335)
      org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:146)
      org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:116)
      org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:121)
      org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invokeHome(ProxyFactoryFinderInterceptor.java:93)
      org.jboss.ejb.EntityContainer.internalInvokeHome(EntityContainer.java:508)
      org.jboss.ejb.Container.invoke(Container.java:894)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:585)
      org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
      org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
      org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
      org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
      org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
      org.jboss.invocation.local.LocalInvoker$MBeanServerAction.invoke(LocalInvoker.java:155)
      org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:104)
      org.jboss.invocation.InvokerInterceptor.invokeLocal(InvokerInterceptor.java:179)
      org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:165)
      org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
      org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:55)
      org.jboss.proxy.ejb.HomeInterceptor.invoke(HomeInterceptor.java:169)
      org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:86)

      I have seen this problem solved on other posts by the use of mysql-get-generated-keys as the entity command, but in my case it does not seem to be helping. I have tried numerous variants of the tags in jbosscmp-jdbc.xml. Here is a snippet from the relevant portion of my current jbosscmp-jdbc.xml file:

      <ejb-name>Subject</ejb-name>

      <table-name>Subject</table-name>

      <cmp-field>
      <field-name>id</field-name>
      <column-name>id</column-name>
      <auto-increment />
      </cmp-field>

      <cmp-field>
      <field-name>name</field-name>
      <column-name>name</column-name>
      </cmp-field>

      <cmp-field>
      <field-name>description</field-name>
      <column-name>description</column-name>
      </cmp-field>

      <entity-command name="mysql-get-generated-keys" />

      Any suggestions are grealy appreciated

        • 1. Re: Null Primary Key
          pittupgd

          Hi spiffer,
          I am getting the same error Have u figured out what the problem was

          • 2. Re: Null Primary Key
            spiffer

            I have not found a solution. I am considering using a different type of generator (sequence, etc) rather than deal with MySQL auto-increment in JBoss. If you end up finding a real solution, I would greatly appreciate it if you would post it here.

            Thanks!

            • 3. Re: Null Primary Key
              rsuffling

              If you're still having problems with this then you might find that it is caused by your MySQL table not having AUTO_INCREMENT specified against it's Primary key, when you created the table. Alter or recreate the table and this should fix it.

              Hope this is of help :-)

              Roger

              • 4. Re: Null Primary Key
                togero

                I'm having the same problem, I do have AUTO_INCREMENT on my PK. No success anyway.

                I check my database and for every call to create a new row is created with a nice autoincremented primary key. The problem is that I get the error:

                Primary key for created instance is null.

                for every call to create.

                /Anders

                • 5. Re: Null Primary Key
                  rsuffling

                  Have you specified

                  <entity-command name="mysql-get-generated-keys" />

                  against each of your entity beans that require auto-generated primary keys in jbosscmp-jdbc.xml?

                  Roger