7 Replies Latest reply on Mar 1, 2004 9:36 AM by sesques

    Problem : CMR field cannot be null

    sesques Novice

      Hi,

      How can I declare the insert strategy (not until ejbPostCreate) with JBOSS. CMR fields cannot be set in ejbCreate but in ejbPostCreate. But JBOSS wants to insert hte item at the end of the ejbCreate method.

      I know very well Weblogic. For thsi server, the tag is <delay-database-insert-until> defined to ejbPostCreate.

      What is the equivalent for JBOSS ?

        • 1. Re: Problem : CMR field cannot be null
          Ingo Bruell Newbie

          In the containerConfiguration is a flag called <sync-on-commit-only> set this to true and synchronisation will happen after or at commit time.

          • 2. Re: Problem : CMR field cannot be null
            Juha Lindfors Master

            insert-after-ejb-post-create in jboss.xml

            • 3. Re: Problem : CMR field cannot be null
              Ingo Bruell Newbie

              Ooops, i was wrong ...

              • 4. Re: Problem : CMR field cannot be null
                sesques Novice

                Hi,

                Thanks for response but cannot work:

                I insert :
                <container-configurations>
                <container-configuration extends="Standard CMP 2.x EntityBean">
                <container-name>foobar</container-name>
                <insert-after-ejb-post-create>true</insert-after-ejb-post-create>
                </container-configuration>
                </container-configurations>
                in my jboss.xml and I threw the exception:
                "Primary key for created instance is null" when I returned from ejbCreate. It's because JBOSS wants to gets the primary key just after the ejbCreate method.

                Another problem is that I dont know how to generate the container configuration with XDoclet. So I have to patch the file jboss.xml each time the file is regenerated.

                In fact, I saw one solution on other forums: I create CMP fields for foreign keys and I initialize them in ejbCreate, then I initialize CMR fields in ejbPostCreate. This solution work, even if it is not in the state of the art.


                • 5. Re: Problem : CMR field cannot be null
                  sesques Novice

                  Of course, to understand all the problem, I forgot to mention that I use database autogenerated keys, thats why jboss threw an error. I think that if not the case, It's works fine with the insert-after-ejb-post-create tag.

                  • 6. Re: Problem : CMR field cannot be null
                    Omar Belakhdar Newbie

                    Could you please post an example of what could be a solution when using auto-increment keys and CMR fieds through foreign keys. I'm having a same problem even by using the <insert-after-ejb-post-create> and setting CMR field at ejbPostCreate method.

                    What is wrong here ?
                    Any idea ?

                    Thanks
                    belakdar.

                    • 7. Re: Problem : CMR field cannot be null
                      sesques Novice

                      Hi,

                      Here is the example :

                      public java.lang.Object ejbCreate( Customer Client,
                      Date OrderDate,
                      String OrderReference)
                      throws CreateException
                      {
                      try {
                      setOrderDate (OrderDate); // Simple CMP field
                      setOrderReference (OrderReference); // Simple CMP field
                      setCustomerId (Client.getId()); // CMP field for foreign key
                      } catch(Exception e) {
                      ...
                      }
                      return null;
                      }

                      public java.lang.Object ejbPostCreate( Customer Client,
                      Date OrderDate,
                      String OrderReference)
                      throws CreateException
                      {
                      try {
                      setCustomer(Client); // Sets the CMR field
                      } catch(Exception e) {
                      ....
                      }
                      }

                      Here is the CMP field for the foreign key

                      /**
                      * @ejb.persistence column-name = "CUSTOMER_ID"
                      * @ejb.interface-method
                      */
                      public abstract Long getCustomerId();

                      /**
                      * @ejb.interface-method
                      */
                      public abstract void setCustomerId(Long val);

                      And the corresponding CMR field

                      /**
                      * @ejb.interface-method
                      * @ejb:transaction type="Required"
                      * @ejb:relation
                      * name="Order-Customer"
                      * role-name="Order-have-Customer"
                      * target-ejb="PhML_Customer"
                      * @jboss:relation
                      * fk-column = "CUSTOMER_ID"
                      * related-pk-field = "customerId"
                      */
                      public abstract PharmaML_EJB.interfaces.Customer getCustomer();
                      /**
                      * @ejb.interface-method
                      */
                      public abstract void setCustomer(PharmaML_EJB.interfaces.Customer arg);