5 Replies Latest reply on May 9, 2005 3:29 PM by robr

    Auto primary key generation with CMP and MySQL

    ian.fallon

      Hi all,

      I'm using JBoss 4.0.0 with MySQL 4.0.18 and have a CMP entity bean which uses an Integer primary key. This is mapped onto an auto-incrementing bigint field in the database. In the Java code I have a create method that does not set the id (PK) field (as it is autogenerated) but when I call the create method I get the following:

      javax.ejb.CreateException: Primary key for created instance is null

      The entry is added to the database OK but the container doesn't seem to understand the auto generated PK.

      I'm obviously missing something here. Any help appreciated.

      Best regards,

      Ian

        • 1. Re: Auto primary key generation with CMP and MySQL
          milowe

          Make sure your in jbosscmp-jdbc.xml contains:

          <unknown-pk>
           <unknown-pk-class>java.lang.Integer</unknown-pk-class>
           <auto-increment/>
          </unknown-pk>
          <entity-command name="mysql-get-generated-keys" class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand"/>


          • 2. Re: Auto primary key generation with CMP and MySQL
            ian.fallon

            Thanks milowe - really appreciate the help - it works find now, you're a star.

            Any recommendations of books that might help with the issues that arise between MySQL and JBoss ? Books on either subject don't really cover this area where the two meet.

            btw. when the books refer to jbosscmp-jdbc.xml I've been assuming this is actually standardjbosscmp-jdbc.xml in the conf directory, or should I not be editing standardjbosscmp-jdbc.xml with this sort of thing ?

            Best regards,

            Ian

            • 3. Re: Auto primary key generation with CMP and MySQL
              milowe

              I dont know of any books focusing in integration MySQL and JBoss, but I am sure they are out there. Reading the manuals of MySQL and JBoss will get you far though. The fun part is then to figure out how to get it to work :) There is no magic about MySQL, it is used like all other data source in JBoss The file standardjbosscmp-jdbc.xml holds definition for the default data source and type mappings between Java, JDBC and the SQL servers being used. This is a server config file. Your deployment specific CMP<->MySQL mappings go into jbosscmp-jdbc.xml in your deployment archive. Preferably you use xdoclet to generate the jbosscmp-jdbc.xml file for you, it makes it a lot easier.

              ?
              * @jboss.persistence
              * datasource = "java:/MySqlDS"
              * datasource-mapping = "mySQL"
              * pk-constraint = "true"
              *
              * @jboss.unknown-pk
              * class="java.lang.Integer"
              * auto-increment="true"
              *
              * @jboss.entity-command
              * name="mysql-get-generated-keys"
              * class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCMySQLCreateCommand"
              ?


              Good luck!


              • 4. Re: Auto primary key generation with CMP and MySQL
                amit10may

                Hi guys,
                I think my problem is similar to yours. But I am using CMR in addition.
                The primary key generation works fine when I done use relationships.
                Can you help me out by viewing the post which describes the issue.

                http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3875593#3875593

                Regards,
                Amit

                • 5. Re: Auto primary key generation with CMP and MySQL
                  robr

                  Could somebody please post the full contents of the jbosscmp-jdbc.xml and ejb-jar.xml files that contain an example of how to use an identity column as a primary key in an entity bean? Also would you please post the script that was used to create the table? I am trying to do this with a table in mssql and have had no luck so far.