1 Reply Latest reply on Jul 4, 2003 2:40 PM by ptk

    CMP & PostgreSQL Auto-Increment

    strupniveralm Newbie

      Does anyone know how to use the Auto-Increment field with JBoss & Postgres?

      I spent the last days to try to use it, but I failed...

      Please, help me =)

      Thanks, Luca

        • 1. Re: CMP & PostgreSQL Auto-Increment
          ptk Newbie

          Here's what seems to work for auto-increments in JBoss 3.2/Postgres 7.3.
          I'm assumming that you have an explicitly defined sequence in the database to automatically increment the primary key in your table on insert.

          In jbosscmp-jdbc.xml:

          For your primary key you need to specify that it auto increments:
          <cmp-field>
          <field-name>id</field-name>
          <column-name>id</column-name>
          <auto-increment/>
          </cmp-field>

          You also must tell JBoss to execute an entity-command for your bean to get the incremented number:
          <entity-command name="get-last-oid" class="org.jboss.ejb.plugins.cmp.jdbc.postgres.JDBCPostgresCreateCommand"/>

          This is just like in the posts I saw regarding MySql, but here the method called is get-last-oid, and it is executed on a class pertaining to Postgres. You can see this method if you browse the jboss.jar file.

          It seems to be working, but I don't yet know if this is the prescribed way of doing this. Also I didn't even specify anything for the auto-increment-template field in the jbosscmp-jdbc.xml. I also didn't use the unknown-pk tag.

          In the ejb code you still need to have the get and set methods for the still mapped primary key field. You won't be using the set method of course. Your create methods also need not take any parameters for the primary key right now.

          I'm not sure how good this solution is, but it seems to be working.

          Best Regards,
          ptk