8 Replies Latest reply on Jul 26, 2004 10:22 AM by tiredofworkingfortheman

    How to deal with AUTO INCREMENT type field?

    llchen

      Hi, I would like to know how to define a CMP bean with the primary-key mapped to the database auto incremented type field? Since this field will be filled automatically by the database when a new record is inserted in. If I define the abstract gettter/setter methods for this field, when the ejbCreate is called, the container will call "insert into " sql statement which will include this field and eventualy will fail.

      I badly need your help!


      Louis

        • 1. Re: How to deal with AUTO INCREMENT type field?
          freefish

          I meet the same problem.

          • 2. Re: How to deal with AUTO INCREMENT type field?
            tdhak

            The way I got round this is to not supply a value for the id in the ejbCreate method. JBoss will then do an insert into the database where the ID CMP field value is NULL, and the database will automatically autoincrement the field.

            Hope this helps.

            Tarwinder Dhak

            • 3. Re: How to deal with AUTO INCREMENT type field?
              llchen

              Thanks, Tarwinder.

              But to create a table with such type of field, I have to create it manualy. Otherwise I don't know how to define it in the deploy script so that the jboss can create it when the table is not there.

              • 4. Re: How to deal with AUTO INCREMENT type field?
                tdang

                Hi all!

                As far as I understand from a lot of posts about this theme on this forum, the auto increment primary key will be supported in JBoss 4.0.

                You can use patterns to implement yourself that are discussed in the book "EJB Design Patterns".

                Hope it helps.

                • 5. Re: How to deal with AUTO INCREMENT type field?
                  matias

                  Hi,
                  I tried doing what you say, but Jboss throws a NullPointerException when the entity bean is created (even though the database is updated properly). I think it's because the id is the primary key of the bean and its set to null.... How did you get around that?

                  Thanks,
                  Matias

                  > The way I got round this is to not supply a value for
                  > the id in the ejbCreate method. JBoss will then do
                  > an insert into the database where the ID CMP field
                  > value is NULL, and the database will automatically
                  > autoincrement the field.
                  >
                  > Hope this helps.
                  >
                  > Tarwinder Dhak

                  • 6. Re: How to deal with AUTO INCREMENT type field?
                    dunuage

                    it seems to me that nobody has searched for the earlier posts before asking?

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

                    • 7. Re: How to deal with AUTO INCREMENT type field?
                      llchen

                      Thanks. As a matter of fact, I am using Hypersonic database. Some one suggest me that I can pass null as the primary key value for the ejbCreate, and the database will generate the auto-numbered key. I haven't tested it yet, but I think it may work.


                      Louis

                      • 8. Re: How to deal with AUTO INCREMENT type field?
                        tiredofworkingfortheman

                        Hi,

                        I am having a problem with CMP and auto-generated keys. I have been routing around the Persistence & CMP/JBoss group here and am not able to come up a solution. I am using jboss 3.2.5 with hypersonic.

                        I have setup the simplest entity bean possible. It has a single primary key that is auto-incremented, it has not any relationships.

                        When I try to create an entity bean I get the error...

                        [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.LogEJB] Could not create entity
                        java.sql.SQLException: Try to insert null into a non-nullable column in statement [INSERT INTO LOGEJB (LogEJB_upk, logCode, message) VALUES (NULL, 'code1111', 'messagei1111')]"

                        My guess is that I have not configured hypersonic correctly in the deployment descripter.

                        when I look at the create statment for the table in the server log. I see...

                        004-07-26 04:03:52,588 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCStartCommand.LogEJB] Executing SQL: CREATE TABLE LOGEJB (LogEJB_upk VARCHAR(32) NOT NULL, logCode VARCHAR(256), message VARCHAR(256), CONSTRAINT PK_LOGEJB PRIMARY KEY (LogEJB_upk))
                        2004-07-26 04:03:52,589 INFO [org.jboss.ejb.plugins.cmp.jdbc.JDBCStartCommand.LogEJB] Created table 'LOGEJB' successfully.
                        2004-07-26 04:03:52,590 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCFindByPrimaryKeyQuery.LogEJB#findByPrimaryKey] SQL: SELECT t0_LogEJB.LogEJB_upk FROM LOGEJB t0_LogEJB WHERE t0_LogEJB.LogEJB_upk=?
                        2004-07-26 04:03:52,591 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCQueryManager.LogEJB] Added findByPrimaryKey query command for local home interface

                        It looks like my create statement is not requesting container managed persistance. Can someone help me with a pointer or some advice?

                        I'm missing something really simple I know it.
                        I have no jbosscmp-gdbc.xml file in my jar file...

                        Here is the ejb-jar.xml snippet...


                        <display-name>LogEJB</display-name>
                        <ejb-name>LogEJB</ejb-name>
                        <local-home>sbn.log.LocalLogHome</local-home>
                        sbn.log.LocalLog
                        <ejb-class>sbn.log.LogBean</ejb-class>
                        <persistence-type>Container</persistence-type>
                        <prim-key-class>java.lang.Object</prim-key-class>
                        False
                        <cmp-version>2.x</cmp-version>
                        <abstract-schema-name>Logs</abstract-schema-name>
                        <cmp-field>
                        no description
                        <field-name>logCode</field-name>
                        </cmp-field>
                        <cmp-field>
                        no description
                        <field-name>message</field-name>
                        </cmp-field>
                        <security-identity>

                        <use-caller-identity></use-caller-identity>
                        </security-identity>


                        ...
                        Here is my create and post create from my LogBean.java file...

                        public java.lang.Object ejbCreate (String code, String message)
                        throws CreateException {

                        Debug.print("LogBean ejbCreate");
                        setLogCode (code);
                        setMessage (message);
                        return null;
                        }

                        public void ejbPostCreate (String code, String message)
                        throws CreateException { }
                        ...

                        Here is my EJBLocalHome.java

                        package sbn.log;

                        import java.util.*;
                        import javax.ejb.*;
                        import java.lang.*;

                        public interface LocalLogHome extends EJBLocalHome {

                        public LocalLog create (String code, String message)
                        throws CreateException;

                        public LocalLog findByPrimaryKey (java.lang.Object id)
                        throws FinderException;
                        }

                        Thanks for any help.