I believe I found a bug performing a mapping for a 1:n association
setting the access attribute of field-descriptor equal to 'anonymous'.
When, I execute the broker.store() method OJB does not store the parent
id in the child table.  The code is listed below.  The code works if I
modify the Child class to store parentId and remove the anonymous tag
from the field-descriptor.  But I am working with existing Java code and
would rather not have to modify it by adding id's in every class that
needs this association in the database.
 
Repository.xml:
<!-- Definitions for Parent -->
<class-descriptor class="Parent" table="PARENT">
      <field-descriptor name="id" column="PARENT_ID" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
      <field-descriptor name="name" column="NAME" jdbc-type="VARCHAR"/>
      <field-descriptor name="age" column="AGE" jdbc-type="INTEGER"/>
      <collection-descriptor name="children"
            element-class-ref="Child"
            auto-update="true"
            orderby="id" sort="DESC">
            <inverse-foreignkey field-ref="parentId"/>
      </collection-descriptor>
</class-descriptor>
<!-- Definitions for Child -->
<class-descriptor class="Child" table="CHILD">
      <field-descriptor name="id" column="CHILD_ID" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
      <field-descriptor name="name" column="NAME" jdbc-type="VARCHAR"/>
      <field-descriptor name="age" column="AGE" jdbc-type="INTEGER"/>
      <field-descriptor name="parentId" column="PARENT_ID"
jdbc-type="INTEGER" access="anonymous"/>
      <reference-descriptor name="parent" class-ref="Parent"
auto-update="true">
            <foreignkey field-ref="parentId"/>
      </reference-descriptor>
</class-descriptor>
 
public class Parent extends Person {      
      Parent()
      {
            super();
            setName("Valerie");
            setAge(33);       
            children = new Vector();
      }
      
      public void addChildren(Child child)
      {
            children.add(child);
      }
      
      public Vector getChildren()
      {
            return children;
      }
      
      private Vector children;
}
 
public class Child extends Person {
      Child()
      {
            super();
            setName("Carmyn");
            setAge(5);
      }
      
      public void setParent(Parent parent)
      {
            this.parent = parent;
      }
      
      public Parent getParent()
      {
            return parent;
      }
      
      private Parent parent;
}
 
public class OJBTester {
 
      public static void main(String[] args) {
            PersistenceBroker broker = null;
            
            try {
                  broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
                  
                  Child camber = new Child();
                  Parent reggie = new Parent();
                  reggie.addChildren(camber);
                  
                  broker.beginTransaction();
                  broker.store(reggie);
                  broker.commitTransaction();
            }
            catch (Throwable t) {
                  t.printStackTrace();
            }
      }
}

Reply via email to