4 Replies Latest reply on Dec 28, 2001 1:48 PM by glm

    CMP bean with auto-increment ID as primary key

    Nitesh Varma Newbie

      Hello,

      I'm developing a CMP entity bean and I want to have an auto-increment identity column of the database table as the primary-key. Can this be done? How do I specify this in the ejb-jar.xml and jaws.xml?

      Thanks,

      Nitesh

        • 1. Re: CMP bean with auto-increment ID as primary key
          David Jencks Master

          No it can't. This is one of the most frequently asked questions, you can find many viewpoints in the forum.

          • 2. Re: CMP bean with auto-increment ID as primary key
            Nathan Newbie

            Here's what I did.

            I used "JBossUtilAutoNumber" which is located at org.jboss.util.AutoNumberFactory.

            In your ejb-jar.xml put:


            <ejb-name>JBossUtilAutoNumber</ejb-name>
            org.jboss.util.AutoNumberHome
            org.jboss.util.AutoNumber
            <ejb-class>org.jboss.util.AutoNumberEJB</ejb-class>
            <persistence-type>Container</persistence-type>
            <prim-key-class>java.lang.String</prim-key-class>
            True
            <cmp-field><field-name>name</field-name></cmp-field>
            <cmp-field><field-name>value</field-name></cmp-field>
            <primkey-field>name</primkey-field>


            In jaws.xml:


            <ejb-name>JBossUtilAutoNumber</ejb-name>
            <table-name>JBossUtilAutoNumberEJBTable</table-name>
            <create-table>true</create-table>
            <remove-table>false</remove-table>
            <tuned-updates>true</tuned-updates>
            <read-only>false</read-only>
            <time-out>300</time-out>
            <cmp-field>
            <field-name>name</field-name>
            <column-name>name</column-name>
            <sql-type>VARCHAR(30)</sql-type>
            <jdbc-type>VARCHAR</jdbc-type>
            </cmp-field>
            <cmp-field>
            <field-name>value</field-name>
            <column-name>value</column-name>
            </cmp-field>



            In your actual EJB (not any of the interfaces), import org.jboss.util.AutoNumberFactory.

            Then in the ejbCreate method:

            id = AutoNumberFactory.getNextInteger("nameofbean");

            Just remember to have id as a member variable of your bean.

            So far this method has been working perfectly and performance has been great.

            • 4. Re: CMP bean with auto-increment ID as primary key
              glm Newbie

              nathanf;

              I have attempted to use your suggestion in my CMP Entity Bean project but when I startup the JBoss server I get the following message:

              ------------

              [Container factory] Deploying JBossUtilAutoNumber
              [Container factory] Deploying Client
              [Container factory] Deploying Employee
              [JAWS] Table 'JBossUtilAutoNumberEJBTable' already exists
              [Container factory] java.lang.NoSuchMethodException
              [Container factory] at java.lang.Class.getMethod0(Native Method)
              [Container factory] at java.lang.Class.getMethod(Class.java:888)
              [Container factory] at org.jboss.ejb.plugins.CMPPersistenceManager.createMethodCache(CMPPersistenceManager.java:122)
              [Container factory] at org.jboss.ejb.plugins.CMPPersistenceManager.init(CMPPersistenceManager.java:102)
              [Container factory] at org.jboss.ejb.EntityContainer.init(EntityContainer.java:291)
              [Container factory] at org.jboss.ejb.Application.init(Application.java:202)
              [Container factory] at org.jboss.ejb.ContainerFactory.deploy(ContainerFactory.java:372)
              [Container factory] at org.jboss.ejb.ContainerFactory.deploy(ContainerFactory.java:304)
              [Container factory] at java.lang.reflect.Method.invoke(Native Method)
              [Container factory] at com.sun.management.jmx.MBeanServerImpl.invoke(MBeanServerImpl.java:1628)
              [Container factory] at com.sun.management.jmx.MBeanServerImpl.invoke(MBeanServerImpl.java:1523)
              [Container factory] at org.jboss.deployment.J2eeDeployer.startModules(J2eeDeployer.java:494)
              [Container factory] at org.jboss.deployment.J2eeDeployer.startApplication(J2eeDeployer.java:468)
              [Container factory] at org.jboss.deployment.J2eeDeployer.deploy(J2eeDeployer.java:208)
              [Container factory] at java.lang.reflect.Method.invoke(Native Method)
              [Container factory] at com.sun.management.jmx.MBeanServerImpl.invoke(MBeanServerImpl.java:1628)
              [Container factory] at com.sun.management.jmx.MBeanServerImpl.invoke(MBeanServerImpl.java:1523)
              [Container factory] at org.jboss.ejb.AutoDeployer.deploy(AutoDeployer.java:379)
              [Container factory] at org.jboss.ejb.AutoDeployer.run(AutoDeployer.java:217)
              [Container factory] at java.lang.Thread.run(Thread.java:484)
              [J2EE Deployer Default] Starting Paye_Payroll-20011228.jar failed!

              -----------------------

              I am not sure from the message whether the problem is with the auto numbering or if it is something else. Is the message :

              [JAWS] Table 'JBossUtilAutoNumberEJBTable' already exists

              normal after the first time you start the server??

              Gary