1 Reply Latest reply on Sep 30, 2003 12:29 PM by Paul Kuit

    Auto-Generated Keys

    Christof Blumstengel Newbie

      is there a usual way to manage it in jboss?

      Sorry, I saw postings about this before, but I'm not able to find them again...

        • 1. Re: Auto-Generated Keys
          Paul Kuit Newbie

          Blumstengel, the whole forum is overloaded with this question!

          I assume you use Xdoclet, if not, you should ;-)

          I also struggled little time with auto-increment keys,
          xdoclet and jboss. it's simpler then it looks...

          Only 1 thing you need to tell XDoclet! All the rest is really the same as normal... except that you don't setId(new UID()) in ejbCreate... of cource.

          Only add to your class tags;

          * @jboss.entity-command
          * name="mssql-get-generated-keys"

          So don't need any unknown-pk stuff at all...

          But read further!

          I read this in other postings already, but they didn't tell me these (get-generated-keys) entity-commands are mapped to CreateCommand classes, defined in standardjbosscmp-jdbc.xml.

          Depending on jboss version (3.2.1 doesn't have this mapping stated above, for mssql), you need to define a mapping yourself;

          1. See package org.jboss.ejb.plugins.cmp.jdbc in jboss.jar for database vendor specific CreateCommand classes)

          2. Define in standardjbosscmp-jdbc.xml, or in xdoclet using
          @jboss.entity-command
          * name="get-generated-keys"
          * class="org.jboss.ejb.plugins.cmp.jdbc.mssql.JDBCMsSQLCreateCommand"

          Obs.
          - the package with CreateCommands classes changed in 3.2.2 (RC4), RC4 gave me transaction problems anyway...
          - the default org.jboss.ejb.plugins.cmp.jdbc.jdbc3.JDBCGetGeneratedKeysCreateCommand didn't work for me... Maybe Microsoft's JDBC driver isn't Type 3 compliant something...


          Okay here my complete code...




          import javax.ejb.CreateException;
          import javax.ejb.EntityBean;
          import javax.ejb.EntityContext;

          import ClientsValue;

          /**
          * @ejb.bean
          * name="Clients"
          * type="CMP"
          * view-type="local"
          * schema="Clients"
          * cmp-version = "2.x"
          * primkey-field = "id"
          * transaction-type = "Container"
          *
          * @ejb.value-object
          * name="Clients"
          * match="*"
          *
          * @ejb.transaction
          * type="Required"
          *
          * @ejb.persistence
          * table-name="Clients"
          *
          * @jboss.persistence
          * create-table="true"
          * remove-table="true"
          *
          * @jboss.entity-command
          * name="mssql-get-generated-keys"
          */

          public abstract class ClientsEJB implements EntityBean {


          /**
          * @ejb.pk-field
          *
          * @ejb.persistent-field
          *
          * @ejb.interface-method
          *
          * @ejb.persistence
          * column-name="clientID"
          *
          * @ejb.transaction
          * type="NotSupported"
          */
          public abstract java.lang.Integer getId();
          public abstract void setId(java.lang.Integer id);

          /**
          * @ejb.persistence
          * column-name="name"
          * @ejb.transaction
          * type="Supports"
          */
          public abstract java.lang.String getName();
          public abstract void setName(java.lang.String name);


          /**
          * @ejb.interface-method
          * @ejb.transaction
          * type="Supports"
          */
          public abstract ClientsValue getClientsValue( );

          /**
          * @ejb.interface-method
          */
          public abstract void setClientsValue( ClientsValue data );


          //==========================================
          // EJB callbacks
          //==========================================

          /**
          * @ejb.create-method
          */
          public Object ejbCreate( ClientsValue data )
          throws CreateException{
          setClientsValue( data );
          return null;
          }

          public void ejbPostCreate( )
          throws CreateException {}

          }



          and added to standardjbosscmp-jdbc.xml (3.2.1);

          <entity-command name="mssql-get-generated-keys"
          class="org.jboss.ejb.plugins.cmp.jdbc.mssql.JDBCMsSQLCreateCommand"/>