Discussions

EJB programming & troubleshooting: JBoss, CMP 2.0 and MySQL auto_increment not working

  1. Hello. I am trying to build a simple example just to see that the technology is working.

    Im using JBuilder X, JBoss 3.23 and MySQL 4.x.
    For the simple example i have a table with few fields, the primary key being type id int(10) unsigned NOT NULL auto_increment

    JBuilder builds the CMP bean automaticly. Then I use DTO/Facade wizard to create dto:s etc for the bean.

    I found a guide http://www.kylev.com/projects/jboss/cmpauto.html for the exact problem and followed it. Still I could not get the thing working. I dont have the exact error message here now, i can post it later.

    But if someone is using Boss, CMP 2.0 and MySQL auto_increment I would be very interested to know how to do it.
  2. Hello Tero,

    I am new to EJB, and I've had just the same problem as you do. I did a little query and found the solution (well, it works for me)...
    So here it is:
    1. The return type of all your ejbCreate() methods in the bean class should by java.lang.Object.
    2. Your bean should not declare the abstract methods for the primary key field (let's call it myId). That means that in your bean class there should not be something like:
    public abstract Object getMyID();
    and
    public abstract void setMyID(Object i_myId);
    3. The input parameter of your findByPrimaryKey() method in your home interface should be of type java.lang.Object. i.e., it should look like:
    public MyReturnType findByPrimaryKey(Object i_key) throws FinderException, RemoteException;
    4. Your standard deployment descriptor (ejb-jar.xml) should not specify the primary key fields (i.e., there should not be something like:
    <cmp-field>
         <field-name>myId</field-name>
    </cmp-field>
    5. Naturlly, this means there should be no "mapping" to this field in your jbosscmp-jdbc.xml (i.e., there should not be something like:
    <cmp-field>
         <field-name>myId</field-name>
         <column-name>MY_ID</column-name>
    </cmp-field>
    6. However, your jbosscmp-jdbc.xml should include the unknown-pk and the entity-command elements (you can look at the jbosscmp-jdbc_3_2.dtd, which is located at the \docs\dtd directory of your JBoss home). All-in-all, your jbosscmp-jdbc.xml should look something like that:
    <jbosscmp-jdbc>
       <defaults>
          ...
       </defaults>

       <enterprise-beans>
          <entity>
             ...

             <unknown-pk>
                <unknown-pk-class>java.lang.Integer</unknown-pk-class>
                <column-name>MY_ID</column-name>
                <jdbc-type>INTEGER</jdbc-type>
                <sql-type>INTEGER</sql-type>
                <auto-increment/>
             </unknown-pk>
             <entity-command name="mysql-get-generated-keys"/>
             ...
          </entity>
       ...
       </enterprise-beans>
    </jbosscmp-jdbc>
    7. Make sure that your jbosscmp-jdbc.xml's DOCTYPE element (at the beggining of the xml file) is referencing and validating the DTD file of JBoss 3.2, or higher (otherwise it will simply not work...). It should be as follwos:
    <!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 3.2//EN" "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_2.dtd">
    (once again, if it is referring the DTD file to JBoss 4.0 instead of 3.2, it should work as well).
    8. Your primary key column in the DB should be of type INTEGER, at the length of 10 digits, not null, and defined to be AUTO_INCREMENT.

    It worked for me. If you have any more questions, you can try to ask, but as I've said earlier, I am new to EJB so I do not know if I'll have all the answers...

    Hope it will help you out.

    Regards,
    Ohad
  3. Hello Ohad,

    your advice it great. I followed it, and it seems to work, but when I describe my table, I see my primary key does not have the 'auto_increment' contraint assigned to it, nor is it a int(10) - it is a varchar(32).

    Any ideas?

    -Best
    Michael