1 Reply Latest reply on Dec 1, 2004 2:56 AM by Alexey Loubyansky

    Using Auto-increment PK from MySQL

    Oskar Matzerath Newbie

      Hello!

      I'm using JBOSS 4.0 with a MySQL DB.

      I try to create a Entity CMP EJB with serveral fields.

      One field is the primary key. Due to this EJB represents a user I want to give primary-keys as Integer values (not timestamps or other generatet stuff).
      I created a column in the db which is of type integer and set to autoincrement (this works fine).

      I createt the following jbosscmp-jdbc.xml:

      <jbosscmp-jdbc>
       <defaults>
       <datasource>java:ForumDS</datasource>
       <datasource-mapping>mySQL</datasource-mapping>
       <entity-command name="mysql-get-generated-keys" />
       </defaults>
      
       <enterprise-beans>
      
       <entity>
       <ejb-name>User</ejb-name>
       <create-table>no</create-table>
       <remove-table>no</remove-table>
      
       <table-name>forum_user</table-name>
      
       <cmp-field>
       <field-name>userId</field-name>
       <column-name>user_id</column-name>
       <jdbc-type>INTEGER</jdbc-type>
       <sql-type>INTEGER</sql-type>
       </cmp-field>
       <cmp-field>
       <field-name>nickName</field-name>
       <column-name>nick_name</column-name>
       <jdbc-type>VARCHAR</jdbc-type>
       <sql-type>VARCHAR</sql-type>
       </cmp-field>
       <cmp-field>
       <field-name>EMail</field-name>
       <column-name>e_mail</column-name>
       <jdbc-type>VARCHAR</jdbc-type>
       <sql-type>VARCHAR</sql-type>
       </cmp-field>
      
       <unknown-pk>
       <unknown-pk-class>java.lang.Object</unknown-pk-class>
       <field-name>userId</field-name>
       <column-name>user_id</column-name>
       <jdbc-type>INTEGER</jdbc-type>
       <sql-type>INTEGER</sql-type>
       <auto-increment />
       </unknown-pk>
       </entity>
       </enterprise-beans>
      </jbosscmp-jdbc>
      
      


      and the ejb-jar.xml looks like:

      
      <ejb-jar >
      
       <description>[CDATA[No Description.]]</description>
       <display-name>Generated by XDoclet</display-name>
      
       <enterprise-beans>
      
       <entity >
       <description>Forum User</description>
      
       <ejb-name>User</ejb-name>
      
       <home>user.UserHome</home>
       <remote>user.User</remote>
       <local-home>user.UserLocalHome</local-home>
       <local>user.UserLocal</local>
      
       <ejb-class>user.UserCMP</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.Object</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>forumUser</abstract-schema-name>
       <cmp-field >
       <description>User Id</description>
       <field-name>userId</field-name>
       </cmp-field>
       <cmp-field >
       <description>Nick Name</description>
       <field-name>nickName</field-name>
       </cmp-field>
       <cmp-field >
       <description>E-Mail Adress</description>
       <field-name>EMail</field-name>
       </cmp-field>
      
       <query>
       <query-method>
       <method-name>findAll</method-name>
       <method-params>
       </method-params>
       </query-method>
       <ejb-ql>[CDATA[SELECT OBJECT(a) FROM forum as a]]</ejb-ql>
       </query>
       </entity>
      
       </enterprise-beans>
       <assembly-descriptor >
       </assembly-descriptor>
      
      </ejb-jar>
      
      


      When I try to create the instance of the EJB an Exception is thrown which says:

      StandardWrapperValve[CreateUser]: Servlet.service() for servlet CreateUser threw exception
      javax.servlet.ServletException: Could not create User! Primary key for created instance is null.
      


      Althoug the row in the db is created.

      can anyone explain why it doesn't work? And why it is unable to bring the data from the db to the Primary key field?

      Thanks
      Oskar