2 Replies Latest reply on Feb 8, 2005 1:35 PM by sbrytskyy

    Auto generating keys in MsSQL Server

    Paulo Albuquerque Newbie

      I'm trying to implement a bean with an auto-generating key using Jboss and MsSQL Server, i've followed what sun's documentation says, here's the ejb-jar.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
      <ejb-jar>
       <enterprise-beans>
       <entity>
       <ejb-name>ClientEJB</ejb-name>
       <local-home>pt.sebasi.client.ejb.LocalClientHome</local-home>
       <local>pt.sebasi.client.ejb.LocalClient</local>
       <ejb-class>pt.sebasi.client.ejb.ClientBean</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.Object</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>Cliente</abstract-schema-name>
       <cmp-field>
       <description>nome do cliente</description>
       <field-name>nome</field-name>
       </cmp-field>
       <cmp-field>
       <description>abreviatura do nome do cliente</description>
       <field-name>abreviatura</field-name>
       </cmp-field>
       </entity>
       </enterprise-beans>
       <assembly-descriptor>
       <container-transaction>
       <description>Transaction attributes for 'ClienteEJB' methods</description>
       <method>
       <ejb-name>ClientEJB</ejb-name>
       <method-name>*</method-name>
       </method>
       <trans-attribute>Required</trans-attribute>
       </container-transaction>
       </assembly-descriptor>
      </ejb-jar>


      the return type for ejbCreate is an Object, and findByPrimaryKey receives an Object as parameter.

      But i can't get this to work, Jboss creates a table column named "ClientEJB_upk" but is unable to store the key, this is the error i get:

      javax.ejb.EJBException: Could not create entity:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert explicit value for identity column in table 'ClientEJB' when IDENTITY_INSERT is set to OFF.


      could someone please help me? thanks in advance

        • 1. Re: Auto generating keys in MsSQL Server
          sesques Novice

          Hi,

          I think You are missing using the @jboss.unknown-pk feature for auto-incremented primary keys:

          Declare the unknown-pk tag:
          * @jboss.unknown-pk
          * class="java.lang.Long"
          * column-name=<your column name>
          * field-name=< the name you want>
          * sql-type=<the type of your primary key>
          * auto-increment="true"

          and declare
          * @jboss.entity-command name = "mssql-fetch-key"

          I hope this helps you.

          • 2. Re: Auto generating keys in MsSQL Server
            sbrytskyy Newbie

            Your jbosscmp-jdbc.xml file must look like:

            <jbosscmp-jdbc>

            java:/MsSQLDataSource
            <datasource-mapping>MS SQLSERVER2000</datasource-mapping>
            <entity-command name="mssql-fetch-key" />

            <enterprise-beans>

            <ejb-name>Customer</ejb-name>
            <table-name>CUSTOMER</table-name>
            <cmp-field>
            <field-name>id</field-name>
            <auto-increment />
            </cmp-field>
            <cmp-field>
            <field-name>lastName</field-name>

            </cmp-field>
            <cmp-field>
            <field-name>firstName</field-name>

            </cmp-field>
            <unknown-pk>
            <unknown-pk-class>java.lang.Long</unknown-pk-class>
            <field-name>id</field-name>
            <auto-increment />
            </unknown-pk>

            </enterprise-beans>
            </jbosscmp-jdbc>