CMR One-to-Many Relationships - Strange Problem


EJB programming & troubleshooting: CMR One-to-Many Relationships - Strange Problem

  1. CMR One-to-Many Relationships - Strange Problem (2 messages)


    I am having a problem that appears top be strange. I have 2 beans, BeanA and BeanB, where BeanA is related to BeanB as follows: one-to-many, unidrectional. Thus, the table for BeanB has a field called BeanAID (which is a foreign key and is non-nullable).

    However, when I try to add a BeanB to BeanA's collection as follows:

    public void addBeanB(BeanBDTO dto) {
      BeanBHome home = // .. getHome
      BeanB b = home.create(dto.getAttr1(), //.. etc) // LINE A

    I get an Insert issue on LINEA above, which states that I require a value for BeanAID (as this is nonnullable).

    I thought that the CMR defined relationship would handle all this logic, but it does not appear to be the case. By the way, as soon as I make the foreign key nullable, everything works as expected. I am using Borlands Enterprise erver 5.0.1 and Borlands Java DB (called JDataStore) for my prototyping.

    What am I missing here?



  2. Hi Chris,

    I've seen this problem before with a relational DB in the background.

    I think my solution was to make the relationship bi-directional, and include bean A in the postCreate of bean B.

    It does seem a little weak though, that your method doesn't get treated as 1 transaction.

  3. Hi Daniel

    I don't think this will work. My understanding is that the Insert statement is executed at the end of the ejbCreate() of the bean, at which stage the foreign key reference is still null.

    What I am thinking of doing is passing through the reference to BeanA to the ejbCreate of BeanB, and introducing a set (mutator) on BeanB to do the update. However, as you mentioned, this will require a bi-directional relationship, but I will simply not expost any getters/setters on BeanB's local/remote interfaces.

    That sounds like it will work.