6 Replies Latest reply on Feb 11, 2005 10:57 AM by pouncilt

    Autoincrement/Identity with CMP - 'Primary key for created i

    lpmon

      I have seen many examples and suggestions and tried them all but none work. I must be missing something. The record is added to the DB correctly but the ejbCreate throws an exception.

      Does anyone know where there is a complete example, end-to-end, with xml files and code? ...and ideally, xdoclet

      It should not be this hard!

        • 1. Re: Autoincrement with CMP - 'Primary key for created instan
          lpmon

          The title got chopped:

          Primary key for created instance is null

          • 2. Re: Autoincrement/Identity with CMP - 'Primary key for creat
            jobor

            I'm using MySQL with a table using an Integer PK field with the autoincrement attribute on. In this code the PK is available after the ejbCreate method because of the <entity-command name="mysql-get-generated-keys"/> and <auto-increment/> elements.
            I'm using XDoclet partly, only for generating the interfaces.

            Johan.

            The bean class

            import javax.ejb.*;
            
            /**
             * @ejb.bean
             * name = "Archive"
             * type = "CMP"
             * view-type = "local"
             * local-jndi-name = "nl/blabla/ArchiveLocal"
             * @ejb.pk
             * class = "java.lang.Integer"
             * generate = "false"
             * @ejb.finder
             * signature = "java.util.Collection findAll()"
             * query = ""
             */
            public abstract class ArchiveBean implements EntityBean {
            
             private EntityContext entityContext;
            
             /**
             * @ejb.create-method
             */
             public Integer ejbCreate(String code, String description) throws CreateException {
             setCode(code);
             setDescription(description);
             return null;
             }
             public void ejbPostCreate(String code, String description) {
             }
             public void ejbActivate() {
             }
             public void ejbPassivate() {
             }
             public void ejbLoad() {
             }
             public void ejbRemove() {
             }
             public void ejbStore() {
             }
             public void setEntityContext(EntityContext entityContext) {
             this.entityContext = entityContext;
             }
             public void unsetEntityContext() {
             entityContext = null;
             }
            
             /**
             * @ejb.interface-method
             */
             public abstract Integer getKey();
             public abstract void setKey(Integer key);
            
             /**
             * @ejb.interface-method
             */
             public abstract String getCode();
             public abstract void setCode(String code);
            
             /**
             * @ejb.interface-method
             */
             public abstract String getDescription();
             public abstract void setDescription(String description);
            }
            

            The ejb-jar.xml
            <!-- Entity Beans -->
            <entity >
             <ejb-name>Archive</ejb-name>
             <local-home>nl.blabla.ArchiveLocalHome</local-home>
             <local>nl.blabla.ArchiveLocal</local>
             <ejb-class>nl.blabla.ArchiveBean</ejb-class>
             <persistence-type>Container</persistence-type>
             <prim-key-class>java.lang.Integer</prim-key-class>
             <reentrant>False</reentrant>
             <cmp-version>2.x</cmp-version>
             <abstract-schema-name>Archive</abstract-schema-name>
             <cmp-field >
             <field-name>key</field-name>
             </cmp-field>
             <cmp-field >
             <field-name>code</field-name>
             </cmp-field>
             <cmp-field >
             <field-name>description</field-name>
             </cmp-field>
             <primkey-field>key</primkey-field>
             <query>
             <query-method>
             <method-name>findAll</method-name>
             <method-params>
             </method-params>
             </query-method>
             <ejb-ql>SELECT OBJECT(a) FROM Archive AS a</ejb-ql>
             </query>
            </entity>
            

            The jbosscmp-jdbc.xml
            <jbosscmp-jdbc>
             <defaults>
             <datasource>java:/mysql-localhost-pop</datasource>
             <datasource-mapping>mySQL</datasource-mapping>
             <create-table>false</create-table>
             <entity-command name="mysql-get-generated-keys"/>
             </defaults>
            
             <enterprise-beans>
            
             <entity>
             <ejb-name>Archive</ejb-name>
             <table-name>t_archive</table-name>
             <cmp-field>
             <field-name>key</field-name>
             <column-name>arc_seq</column-name>
             <auto-increment/>
             </cmp-field>
             <cmp-field>
             <field-name>code</field-name>
             <column-name>arc_code</column-name>
             </cmp-field>
             <cmp-field>
             <field-name>description</field-name>
             <column-name>arc_description</column-name>
             </cmp-field>
             </entity>
            


            • 3. Re: Autoincrement/Identity with CMP - 'Primary key for creat
              lpmon

              Thanks Johan!

              I am using hsqldb for now but may be using MySQL.

              I need to find the equivalent of "mysql-get-generated-keys" for the hsqldb.

              • 4. Re: Autoincrement/Identity with CMP - 'Primary key for creat
                lpmon

                The link below seems to be helpful. After adding generated keys to my prior searches it turned up.

                http://www.coredevelopers.net/library/jboss/cmp/keygen.jsp

                • 5. Re: Autoincrement/Identity with CMP - 'Primary key for creat
                  pouncilt

                  lpmon - I know it was a long time ago when you was inquiring about the auto-increment option in mySQL, but I just figured it out. And you are right its not that hard...

                  I have been searching all over the internet and jboss forums and everybody seem to be on the same page when it comes to the jbosscmp-jdbc.xml file. Mines look like this...

                  <jbosscmp-jdbc>

                  java:/MySqlDS
                  <datasource-mapping>mySQL</datasource-mapping>
                  <create-table>true</create-table>
                  <entity-command name="mysql-get-generated-keys"/>


                  <enterprise-beans>

                  <ejb-name>AdminEJB</ejb-name>
                  <table-name>Admin</table-name>
                  <cmp-field>
                  <field-name>id</field-name>
                  <column-name>id</column-name>
                  <auto-increment/>
                  </cmp-field>
                  .
                  .
                  .

                  <unknown-pk>
                  <unknown-pk-class>java.lang.Integer</unknown-pk-class>
                  <column-name>ORDER_ID</column-name>
                  <jdbc-type>INTEGER</jdbc-type>
                  <sql-type>INTEGER</sql-type>
                  <auto-increment/>
                  </unknown-pk>
                  <entity-command name="mysql-get-generated-keys"/>


                  Given that I have been banging my head over this for 2 days now with about 4 hours of sleep, I have not had a changes to play around with the unknown-pk tag. I believe it is okay to leave this part out, if you primary key is a not a join of two or more columns. I will play around with that tomarrow. But for now I am leaving it in.

                  Now here is the Gotcha part that no one is tell people:
                  My guess is they really don't know what they know. They just some how lucked up a it work for them with out too much headace.

                  If JBoss is able to insert data into your MySql Database and generate the auto increment fields, but you are getting a EJBCreate Exception, then your problem are using the wrong driver. I a using mysql-connector-java-3.1.6 driver.

                  But here is another gotcha:
                  There is aleast two different type of drivers in this distribution. If you are having problems then most likely you are using com.mysql.jdbc.Driver or some other driver in the package. The correct on to use is org.gjt.mm.mysql.Driver.

                  One last then I would drop any previously created table to make sure the table are create correctly when JBoss deploys your EJB.

                  Holla!

                  pouncilt - etnot96@yahoo.com



                  • 6. Re: Autoincrement/Identity with CMP - 'Primary key for creat
                    pouncilt

                    FYI, I have actually test this configuration and found that I didn't need the unknow primary key stuff if I am using one column for a primary key.


                    Hope this helps someone!

                    pouncilt