5 Replies Latest reply on Apr 2, 2005 11:28 AM by websel

    Postgresql 7.4.x/8.0, CMP and auto-increment

    dtn70437

      Hi,

      could somebody tell me how the use auto-increment PK's
      for entity-beans with jboss ?
      a) How to declare the entity bean with XDoclet ?
      b) How do i get the new PK after creating Bean ? I want to
      return this PK to the client ..

      I search in den Newsgroups/Forums ... but i missed the answer - Sorry

      regards

      Danny

        • 1. Re: Postgresql 7.4.x/8.0, CMP and auto-increment
          hfarid

          Hi
          Based on my search in the forum I gathered the following

          - On the entity level you need to specify the following tag

          @jboss.entity-command
          * name="informix-serial"
          * class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCInformixCreateCommand"

          in your case name = postgresql-fetch-seq and the class would be org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCPostgreSQLCreateCommand

          an on you getId() method you need to specify the following tag @jboss.persistance auto-increment ="true"

          you can see the result of these two steps in your jbosscmp-jdbc.xml
          on the entity you will get <enitiy-command ....> and on the field you will get the <auto-increment/>


          I did all of that for my entity and I mange to deploy it successfully BUT whenevr I try to create an new enity.. I get the following exception

          22:17:50,126 ERROR [MyEntity] Could not create entity
          java.sql.SQLException: Primary key on table (mytable) has a field with a null key value.
          at com.informix.jdbc.IfxSqli.addException(IfxSqli.java:3082)
          at com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3396)
          at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2259)
          at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2179)
          at com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:721)
          at com.informix.jdbc.IfxResultSet.executeUpdate(IfxResultSet.java:305)
          at com.informix.jdbc.IfxStatement.executeUpdateImpl(IfxStatement.java:882)


          From the postings I feel that these two steps are enough (give it a try it might work for you)


          • 2. Re: Postgresql 7.4.x/8.0, CMP and auto-increment
            dtn70437

            Whow, great, your solutions work great !! Thx

            Could you tell me also

            b) How to get the PK in the createMethode to return it to the client ?

            regards

            Danny

            • 3. Re: Postgresql 7.4.x/8.0, CMP and auto-increment
              hfarid

              take a look at my other posting on the Informix/auto-increment

              http://www.jboss.com/index.html?module=bb&op=viewtopic&t=61244

              You don't have to return anything in ejbCreate(), just return null

              lets say you are creating your entity bean from a session bean using blaHome.create() which returns blaLocal. you can always get the primary key on that object.



              • 4. Re: Postgresql 7.4.x/8.0, CMP and auto-increment
                dtn70437

                Hi,

                thanks again. Now the Bean works. Here is the summary (XDoclet):

                Bean-Xdoclet-Descriptor
                
                /**
                 * @ejb.bean name="Kunde" display-name="Name for KundeBean"
                 * description="Description for KundeBean" jndi-name="ejb/Kunde"
                 * type="CMP" cmp-version="2.x" view-type="local"
                 *
                 * @ejb.pk class = "java.lang.Object" generate = "false" ejb.value-object match =
                 * "*" name = "KUNDE"
                 *
                 * @ejb.value-object match = "*" name = "Kunde"
                 *
                 * @jboss.persistence table-name = "KUNDE" read-only = "false" create-table =
                 * "false" remove-table = "false"
                 *
                 * @jboss.entity-command name="postgresql-fetch-seq"
                 * class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCPostgreSQLCreateCommand"
                 *
                 * @jboss.unknown-pk auto-increment = "true" column-name = "KUNDE_ID" jdbc-type =
                 * "BIGINT" sql-type = "INT8" class = "java.lang.Object"
                 *
                 */



                Entity Bean Methods
                
                /**
                 * @ejb.create-method view-type = "local"
                 *
                 * @throws CreateException
                 */
                 public java.lang.Object ejbCreate(String firmenname, String strasse,
                 String plz, String ort, String telefon, String fax,
                 String webSeiteURL, String email) throws CreateException {
                // ....
                return null;
                }
                
                // Important: NO Setter/Getter/Xdoclet stuff for the primary-field !!!!!
                


                Using Session Bean creating the entity bean
                /**
                 * @ejb.interface-method view-type = "both"
                 */
                 public java.lang.Object createKunde(String firmenname, String strasse,
                 String plz, String ort, String telefon, String fax,
                 String webSeiteURL, String email) throws ECannotCreateException {
                
                 KundeLocal myKundeLocal = null;
                
                 try {
                 Logger.log("KundeMgrBean.createKunde()");
                 myKundeLocal = this.myKundeLocalHome.create(firmenname, strasse,
                 plz, ort, telefon, fax, webSeiteURL, email);
                
                 Logger.log("Neue Kundenid: " + myKundeLocal.getPrimaryKey());
                 return myKundeLocal.getPrimaryKey();
                
                 } catch (CreateException e) {
                 throw new ECannotCreateException("Cannot create 'Kunde' "
                 + firmenname, e);
                 }
                 }
                


                • 5. Re: Postgresql 7.4.x/8.0, CMP and auto-increment
                  websel

                  > // Important: NO Setter/Getter/Xdoclet stuff for the primary-field !!!!!

                  If i leave these, how could i ever build CMR's ?
                  I've been looking for a solution and on the following posting a sugestion is posted however during compilation it brakes.

                  http://www.jboss.org/index.html?module=bb&op=viewtopic&t=54267

                  It breaks as Jboss generates a line:

                  pk = new java.lang.Object(this.getId());

                  Which can't be compiled under java 5.0 or is simply wring ..

                  Could anybody please provide me how to get CMR working here?

                  Wessel de Roode