1 Reply Latest reply on Mar 17, 2006 10:43 AM by tterm

    EntiyBean and auto-increment

    modendahl

      Hi,

      after reading a lot of stuff about the item. I don't know what is the
      right way.
      Here is my problem.
      I'm using Jboss 4.0.3 with a MySQL 4.1 with tables which use
      the serial type as primary key.
      I want to use entity beans which make use of the auto-increment feature.
      I'm using xdoclet. Before I start I want to know which is the right way.
      These are the things I found.
      1. Solution
      In the head of the bean

       * @jboss.entity-command name="mysql-get-generated-keys"
       *
       * @jboss.unknown-pk class="java.lang.Integer"
       * auto-increment="true"
      

      Do I have do declare the column of the table with the primary key
      as field in the bean?

      The create method looks like
      /**
       * @ejb.create-method
       * @return Primary Key
       * @throws CreateException
       */
       public Integer ejbCreate() throws CreateException {
       return null;
       }
      
       public void ejbPostCreate() {
      
       }
      


      2. Solution
      I only declare in the head of the bean the following
       * @jboss.entity-command name="mysql-get-generated-keys"
       *
      


      And the method declaration for the primary key is
       /**
       * Getter for CMP Field id
       *
       * @ejb.pk-field
       * @ejb.persistent-field
       * @ejb.interface-method view-type="both"
       *
       * @jboss.persistence auto-increment="true"
       *
       */
       public abstract java.lan.Integer getId();
      

      The create method looks the same as in solution 1.

      What is the right way?

      Another problem is how can I access the bean when the key is
      autogenerated? The create method delivers a null.
      How can I access the generated key?

      Thanks in advance,

      Marcel.

        • 1. Re: EntiyBean and auto-increment
          tterm

          Hello,

          this is a possible solution for me:

          /**
           * @ejb.bean name="Example"
           * display-name="Name for Example"
           * description="Description for Example"
           * jndi-name="ejb/Example"
           * type="CMP"
           * cmp-version="2.x"
           * view-type="local"
           * primkey-field = "autoIncField"
           *
           * @ejb.pk class = "java.lang.Integer" generate = "false"
           *
           * @jboss.unknown-pk class = "java.lang.Integer"
           * column-name = "autoIncField"
           * jdbc-type = "INTEGER"
           * sql-type = "INTEGER"
           * auto-increment = "true"
           *
           *
           * @jboss.entity-command name = "mysql-get-generated-keys"
           * class = "org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand"
           *
           */
          public abstract class ExampleBean implements EntityBean {
          
           // more methods
          
          /**
           * Getter for CMP Field autoIncField
           *
           * @ejb.pk-field
           * @ejb.persistent-field
           * @ejb.interface-method view-type="local"
           *
           * @jboss.persistence auto-increment = "true"
           *
           */
           public abstract java.lang.Integer getAutoIncField();
          
           /**
           * Setter for CMP Field autoIncField
           *
           *
           * @ejb.interface-method view-type="local"
           */
           public abstract void setAutoIncField(java.lang.Integer value);
          }
          


          This works for me.

          How would you access a row in a table with a generated PK with a simple sql statement if you don't have the key value. I guess you have some other data in the row for a search criteria. I hope you know what I mean.

          Thomas