1 Reply Latest reply on Nov 10, 2003 12:06 AM by Juha Lindfors

    Issue creating CMP Beans with not null CMR

    prashan Newbie

      Hi all,

      I recently stepped into a problem which doesn't do the create method if you have a not null CMR to handle with. In JBoss versions 3.2.1 and earlier, the solution was to set the not null relation through a trigger in a table. But, with 3.2.2 release, they say that the problem has been fixed. This version has a new entry in the standardjboss.xml file which is <insert-after-ejb-post-create>. We can use this to tell the container that the actual insert should be called to the database after processing the ejbPostCreate() method. This gives us the chance to update the not null CMR before the container writes the row in the table.

      This is a very good solution to the problem I was having for couple of days and it works fine if you are passing the primary key as well in the create method. But, there's more. If the CMP Bean has a auto generated primary key field, we can tell the container to use a specific entity-command to get the next sequence from the table and do the insert with the primary key from the sequence. There are two ways to do this also.

      We can use two entity-commands. I will give examples to both of them below.
      In standardjbosscmp-jdbc.xml, we can define the entity-commands as follows :
      1.
      <entity-command name="Sequence1"
      class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCOracleCreateCommand">
      AVA_MISC_CHR_SEQ1
      </entity-command>

      2.
      <entity-command name="pk-sql"
      class="org.jboss.ejb.plugins.cmp.jdbc.keygen.JDBCPkSqlCreateCommand">
      <!-- change to define SQL used to obtain key prior to insert -->
      SELECT AVA_MISC_CHR_SEQ1.NEXTVAL FROM DUAL
      </entity-command>


      From these two options, I found that the new feature of JBoss 3.2.2 only works with the 2nd example. The first method will not create an entity. It will give an error like this :
      javax.ejb.CreateException: Primary key for created instance is null.

      If I set the <insert-after-ejb-post-create> to false, then the container will give an error saying that it cannot insert null into a not null foreign key field, which I think because, the container tries to do the insert before executing the ejbPostCreate() method.

      All these problems are solved if we use the 2nd option for the entity command. This command uses a different method to retrieve the auto generated sequence number from the table.

      I think, the new feature is not fully tested on JBoss 3.2.2, otherwise, it should work on both occations.

      I hope this issue will be fixed in the coming version of JBoss.

      Thanks,

      Prashan.