1 Reply Latest reply on Dec 29, 2005 12:47 PM by Benoit Meilleur

    Help with CMR and not null foreign key

    Simon Oldfield Newbie

      Hi!

      I am currently attempting to implement a container managed relationship between 2 beans. This is a many-to-one relationship between BeanA and BeanB implemented as a foreign key field, the end result being that I can call BeanA.getB() method and have it return me the related BeanBLocal instance.

      So, I have beans A & B where bean A is related to a single bean B. That is...

      BeanA
      - id INT PK
      - b_id INT FK to B.id NOT NULL

      BeanB
      - id INT PK

      BeanB exists prior to creating a BeanA, so I pass a BeanBLocal into the ejbCreate and ejbPostCreate of BeanA. In the ejbPostCreate, I call the setBeanB method.

      The first problem I came across was that the ejbCreate would fail due to the the b_id field being defined as NOT NULL in the database - something we DO want. To get around this, I found the insert-after-ejb-post-create tag and defined this to be true!

      With this set to true, I am now having the problem that I simply cannot create any entities. They all fail with a javax.ejb.CreateException: Primary key for created instance is null. The reason (I think it is anyway) is that all my objects have an id primary key field defined to come from a database sequence "automagically". That is, I never pass in a primary key to any ejbCreate methods, but rather use the @jboss.persistence auto-increment="true" tag.

      Does this make sense?

      Any ideas on how to achieve this result?

      I am using jboss 3.2.5 and oracle 9i.

      Any help MUCH appreciated.

        • 1. Re: Help with CMR and not null foreign key
          Benoit Meilleur Newbie

          Hi Simon,

          I'm having a problem similar to the one you had a year ago. I'm with JBoss 4.0.3 and having some CMP 2.0 deployed and when inserting I get the javax.ejb.CreateException: Primary key for created instance is null error.

          On creation I always pass a null value as I did put the oracle sequence in my jbosscmp-jdbc.xml file for my entity. This is happening since I put INSERT-AFTER-EJB-POST-CREATE to true in the standardjboss.xml

          Did you solve the issue ??

          Thanks a lot,

          Ben