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'>
       <description>nome do cliente</description>
       <description>abreviatura do nome do cliente</description>
       <description>Transaction attributes for 'ClienteEJB' methods</description>

      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


          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:


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


            <auto-increment />


            <auto-increment />