1 Reply Latest reply on Mar 3, 2004 6:56 AM by sesques

    CMP:auto-pk-increment problem with PostgreSQL

    mandarjboss

      I am trying to implement auto-increment of primary key feature of JBoss3.2.

      I am giving following code and allowing JBoss to auto-create the tables.
      But i am still getting the exception as,

      java.sql.SQLException: ERROR: ExecInsert: Fail to add null value in not null attribute id

      /*
      * Created on 25-feb-2004
      *
      */
      package questionbank.ejb;
      import javax.ejb.CreateException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;

      /**
      *
      * @ejb.bean
      * cmp-version = "2.x"
      * jndi-name = "CustomerBean"
      * name = "Customer"
      * primkey-field = "id"
      * schema = "CustomerSchema"
      * type = "CMP"
      * view-type = "remote"
      *
      * @ejb.home remote-class = "ejb.CustomerHome"
      * @ejb.interface remote-class = "ejb.Customer"
      *
      * @ejb.persistence
      * table-name = "Customer"
      *
      * @jboss.entity-command
      * name = "postgresql-fetch-seq"
      *
      *
      */
      public abstract class CustomerBean implements EntityBean {

      /**
      * @param email
      * @param password
      * @param lastname
      * @param firstname
      * @return
      * @throws CreateException
      *
      * @ejb.create-method
      */
      public Integer ejbCreate(String email, String pwd,String lastname, String firstname) throws CreateException {
      setEmail(email);
      setPassword(password);
      setLastname(lastname);
      setFirstname(firstname);
      return null;
      }

      /**
      * @throws CreateException
      */
      public void ejbPostCreate(String email, String password, String lastname, String firstname) throws CreateException {
      }

      /**
      * @return
      *
      * @ejb.interface-method
      * @ejb.pk-field
      * @ejb.persistence
      * column-name = "id"
      * sql-type = "INT"
      * @jboss.persistence
      * auto-increment = "true"
      */
      public abstract Integer getId();

      /**
      * @param id
      */
      public abstract void setId(Integer id);

      /**
      * @return
      *
      * @ejb.interface-method
      * @ejb.persistence
      * column-name = "email"
      * sql-type = "varchar"
      * @ejb.persistent-field
      */
      public abstract String getEmail();

      /**
      * @param email
      *
      * @ejb.interface-method
      */
      public abstract void setEmail(String email);

      /**
      * @return
      *
      * @ejb.interface-method
      * @ejb.persistence
      * column-name = "password"
      * sql-type = "varchar"
      * @ejb.persistent-field
      */
      public abstract String getPassword();

      /**
      * @param password
      *
      * @ejb.interface-method
      */
      public abstract void setPassword(String password);

      /**
      * @return
      *
      * @ejb.interface-method
      * @ejb.persistence
      * column-name = "lastname"
      * sql-type = "varchar"
      * @ejb.persistent-field
      */
      public abstract String getLastname();

      /**
      * @param lastname
      *
      * @ejb.interface-method
      */
      public abstract void setLastname(String lastname);

      /**
      * @return
      *
      * @ejb.interface-methodB
      * column-name = "firstname"
      * sql-type = "varchar"
      * @ejb.persistent-field
      */
      public abstract String getFirstname();

      /**
      * @param firstname
      *
      * @ejb.interface-method
      */
      public abstract void setFirstname(String firstname);

      }


      In my standardjbosscmp-jdbc.xml i have the following,

      <entity-command name="postgresql-fetch-seq" class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCPostgreSQLCreateCommand">

      </entity-command>






        • 1. Re: CMP:auto-pk-increment problem with PostgreSQL
          sesques

          Hi,

          You must use the @jboss.unknown-pk feature for auto-incremented primary keys:

          Remove the getter and setter methods for your primary key id.
          Remove the primkey-field in the @ejb.bean tag

          Declare the primary key class as object:
          * @ejb.pk class = "java.lang.Object"

          Declare the unknown-pk tag:
          * @jboss.unknown-pk
          * class="java.lang.Long"
          * column-name="id"
          * field-name="id"
          * sql-type="INT"
          * auto-increment="true"

          Change the returned type of ejbCreate in "java.lang.Object"

          I hope this helps you.