6 Replies Latest reply on Jun 2, 2006 9:16 AM by lauri

    Automatic generation of primary key

    iraten

      Hi,

      I am migrating an application from Jonas to JBoss 4.0.

      In my jonas-ejb-jar.xml I have <automatic-pk>true</automatic-pk> field for the automatic generation of primary key.

      I am looking for the equivalent jboss field for jbosscmp-jdbc.xml.

      Can you help me?

      Thank you

        • 1. Re: Automatic generation of primary key
          lafr

          Look for auto-increment and entity-command.

          • 2. Re: Automatic generation of primary key
            iraten

            Thank you for your answer.

            It was the way I found.

            I use

            <entity-command name="key-generator"
             class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCKeyGeneratorCreateCommand">
            <attribute name="key-generator-factory">
            HiLoKeyGeneratorFactory
            </attribute>
            
            </entity-command>



            Unfortunatly, I get a ClassCastException because it generates a Long and I need an Integer.

            Do you now the name of an appropriate factory?

            Thank you



            • 3. Re: Automatic generation of primary key
              lauri

              Do you use xdoclet for .xml generation?

              I do, so for pk of Integer my attributes are:
              class level

              /**
              @jboss.entity-command name="mysql-get-generated-keys"
              @ejb.bean primkey-field="id" ... also other settings
              */
              class ....
              


              method level
              /**
              * @ejb.pk-field
              * @jboss.persistance auto-increment="true"
              */
              public abstract Integer getId();
              


              This results in
              jbosscmp-jdbc-xml
              <entity>
               <ejb-name>MyEJB</ejb-name>
               <cmp-field>
               <field-name>id</field-name>
               <autoincrement/>
               .....
              </cmp-field>
              ....
              <entity-command name="mysql-get-generated-keys"></entity-command>
              </entity>
              


              ejb-jar.xml is common.

              • 4. Re: Automatic generation of primary key
                lauri

                Answer related to mysql indeed.
                But there is similar support for other common databases .

                • 5. Re: Automatic generation of primary key
                  iraten

                  Thank you for your reply.

                  I've tried your solution using

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

                  and

                  <autoincrement/>


                  Unfortunatly, it doesn't solve my problem (I still get a null primary key).
                  Did you do something else in order to find this class?(maybe adding a jar or a reference to a jar....)?

                  Thank you again



                  • 6. Re: Automatic generation of primary key
                    lauri

                    I don't know what it could be then :(.

                    As far as i understand, general idea of pk generation is to tell EJB engine what type (class) of pk it will be, and tell details to jboss personally.

                    Something about integer type should be mentioned in ejb-jar.xml and particular instructions in jbosscmp-jdbc.xml

                    How your ejbCreate and ejbPostCreate looks like?

                    It should not contain pk field in signature and no calls to pk inside i mean nothing of
                    getId() (could be not assigned yet)
                    and
                    setId(id) will override autoincremented value.

                    That is just my current understanding, it works for me.