1 Reply Latest reply on Apr 4, 2004 1:08 AM by Juha Lindfors

    auto-increment CMP entity bean in Hypersonic

    jlub Newbie

      Ok, trying to do something VERY simple here, I can't believe no one has done this already.

      I'm trying to implement a very simple container-managed-persistent Entity Bean in JBoss 3.2.3 using the built-in Hypersonic database. I want the bean to have an auto-incremented primary key field.

      In fact, by hand-creating the table with the primary key field as an 'IDENTITY' field, I have managed to get it so when you create the bean, it does actually create the row in the table with the correct auto-incremented value, but I then get this Exception:

      javax.ejb.CreateException: Primary key for created instance is null.
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createEntity(JDBCStoreManager.java:556)
      at org.jboss.ejb.plugins.CMPPersistenceManager.createEntity(CMPPersistenceManager.java:208)
      at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.createEntity(CachedConnectionInterceptor.java:269)
      at org.jboss.ejb.EntityContainer.createHome(EntityContainer.java:737)

      (etc., etc.)

      I have not been able to figure out why I'm getting this exception or how to prevent it.

      I'm not sure what parts of my code to paste here, there's basically no code involved. My ejbCreate is:

      public Integer ejbCreate(String name) throws CreateException
      {
      setName(name);
      return new Integer(0);
      }

      Where 'name' is another field in the bean (other than the pk field). I returned a zero in a failed attampt to prevent the exception.

      I'm also using Eclipse and JBoss-IDE.

      Help!

      --Josh

        • 1. Re: auto-increment CMP entity bean in Hypersonic
          Juha Lindfors Master

          This works for me:

          jbosscmp-jdbc.xml

          <enterprise-beans>
          
           <entity>
           <ejb-name>Books</ejb-name>
           <datasource>java:/DefaultDS</datasource>
           <datasource-mapping>Hypersonic SQL</datasource-mapping>
           <pk-constraint>false</pk-constraint>
           <unknown-pk>
           <unknown-pk-class>java.lang.Integer</unknown-pk-class>
          
           <column-name>ID</column-name>
           <jdbc-type>INTEGER</jdbc-type>
           <sql-type>INTEGER</sql-type>
           <auto-increment/>
           </unknown-pk>
           <entity-command name="hsqldb-fetch-key"/>
           </entity>
          
          </enterprise-beans>