db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Dudziak <to...@first.fhg.de>
Subject Re: Referencing an abstract class. The next turn...
Date Tue, 12 Oct 2004 09:30:19 GMT
Carsten Spräner_Ext wrote:

>Hello Tom,
>
>here is my repository_test.xml:
>
><class-descriptor class="db.A" table="A" isolation-level
>="read-uncommitted">
>  <extent-class class-ref="db.B"/>
>  <field-descriptor name="id" id="1" column="ID" primarykey="true"
>autoincrement="true" jdbc-type="BIGINT" />
>  <field-descriptor name="aAttr" id="1" column="A_ATTR" jdbc-type="VARCHAR"
>/>
>  <field-descriptor name="colId" jdbc-type="BIGINT" access="anonymous"
>column="COL_ID" />
></class-descriptor>
>
><class-descriptor class="db.B" table="B" isolation-level
>="read-uncommitted">
>  <field-descriptor name="id" id="1" column="ID" primarykey="true"
>autoincrement="true" jdbc-type="BIGINT" />
>  <field-descriptor name="superID" id="1" column="SUPER_ID" jdbc-type
>="BIGINT" access="anonymous"/>
>  <field-descriptor name="bAttr" id="2" column="B_ATTR" jdbc-type="VARCHAR"
>/>
>  <reference-descriptor name="super"
>    auto-update="true"
>    auto-retrieve="true"
>    class-ref="db.A">
>     <foreignkey field-ref="superID"/>
>  </reference-descriptor>
></class-descriptor>
>
><class-descriptor class="db.Col" table="COL" isolation-level
>="read-uncommitted">
>  <field-descriptor name="id" id="1" column="ID" primarykey="true"
>autoincrement="true" jdbc-type="BIGINT" />
>  <collection-descriptor
>    name="aCol"
>    auto-retrieve="true"
>    auto-update="true"
>    element-class-ref="db.A"
>  >
>    <inverse-foreignkey field-ref="colId"/>
>  </collection-descriptor>
></class-descriptor>
>  
>
Hmm, since you have an explicit <extent-class> in the descriptor db.A, 
you don't need either superID or the super-reference. In fact, I think 
they give you problems instead. (Btw, you don't need the 'id' XML 
attribute for your fields either, I think.)
Also, you shouldn't use anonymous fields for collection foreign keys 
(field colId) because that is not well supported. Also this field has to 
be in db.B, too.
So try this:

<class-descriptor class="db.A"
                  table="A"
                  isolation-level="read-uncommitted">
  <extent-class class-ref="db.B"/>
  <field-descriptor name="id"
                    column="ID"
                    primarykey="true"
                    autoincrement="true"
                    jdbc-type="BIGINT" />
  <field-descriptor name="aAttr"
                    column="A_ATTR"
                    jdbc-type="VARCHAR"/>
  <field-descriptor name="colId"
                    jdbc-type="BIGINT"
                    column="COL_ID" />
</class-descriptor>

<class-descriptor class="db.B"
                  table="B"
                  isolation-level="read-uncommitted">
  <field-descriptor name="id"
                    column="ID"
                    primarykey="true"
                    autoincrement="true"
                    jdbc-type="BIGINT" />
  <field-descriptor name="bAttr"
                    column="B_ATTR"
                    jdbc-type="VARCHAR"/>
  <field-descriptor name="colId"
                    jdbc-type="BIGINT"
                    column="COL_ID" />
</class-descriptor>

<class-descriptor class="db.Col"
                  table="COL"
                  isolation-level="read-uncommitted">
  <field-descriptor name="id"
                    column="ID"
                    primarykey="true"
                    autoincrement="true"
                    jdbc-type="BIGINT" />
  <collection-descriptor
    name="aCol"
    auto-retrieve="true"
    auto-update="true"
    element-class-ref="db.A"
  >
    <inverse-foreignkey field-ref="colId"/>
  </collection-descriptor>
</class-descriptor>

See here for details on mapping via <extent-class> or via multiple 
joined tables:

http://db.apache.org/ojb/docu/guides/advanced-technique.html#Mapping+Inheritance+Hierarchies

>I got the first error fixed in the ClassDesriptor. In the method
>getFieldDescriptorByName it only returns the FieldDescriptors of its own.
>It does not look in the baseClass if it has one. The following code fixes
>this problem:
>
>    /**
>     * Returns the matching {@link FieldDescriptor}.
>     */
>    public FieldDescriptor getFieldDescriptorByName(String name)
>    {
>        if (name == null || m_FieldDescriptions == null)
>        {
>            return null;
>        }
>
>        if (m_fieldDescriptorNameMap == null)
>        {
>            HashMap nameMap = new HashMap();
>
>            FieldDescriptor[] descriptors = getFieldDescriptions();
>            for (int i = descriptors.length - 1; i >= 0; i--)
>            {
>                FieldDescriptor fld = descriptors[i];
>                nameMap.put(fld.getPersistentField().getName(), fld);
>            }
>
>            m_fieldDescriptorNameMap = nameMap;
>        }
>        // OLD: return (FieldDescriptor)
>m_fieldDescriptorNameMap.get(name);
>        /****************** FIX ***********************/
>        FieldDescriptor fd = (FieldDescriptor)
>m_fieldDescriptorNameMap.get(name);
>        if( fd == null ) {
>            String baseClass = getBaseClass();
>            if( baseClass == null ) return null;
>            ClassDescriptor cld  = getRepository().getDescriptorFor(
>baseClass );
>            fd = cld.getFieldDescriptorByName( name );
>        }
>        return fd;
>    }
>  
>
You certainly shouldn't need to have to modify OJB for your ends. 
Mapping abstract classes/interfaces is well supported (because everybody 
uses them :-) ).

Tom


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message