Hey All,

[Using OJB_1_0_RELEASE branch from CVS]

As other users here, I'm also struggling with mapping class hierarchy on multiple joined tables, and can't figure out what's wrong and whether it is working at all or not.

Consider this slightly modified (added relation to the class C) example from the OJB manual page [1]:

/**
 * @ojb.class
 * @ojb.extent-class class-ref="B"
 */
public class A {
    /** @ojb.field primarykey="true" autoincrement="ojb" */
    Integer id;
    /** @ojb.field */
    Integer cId;
    /** @ojb.reference foreignkey="cId" */
    C c;
}

/**
 * @ojb.class include-inherited="false"
 * @ojb.reference class-ref="A" foreignkey="aId"
 *     auto-retrieve="true" auto-update="object" auto-delete="object"
 */
public class B extends A
{
    /** @ojb.field primarykey="true" autoincrement="ojb" */
    Integer id;
    /** @ojb.field */
    Integer aId;
    /** @ojb.field length="254" */
    String value;
}

/**
 * @ojb.class
 */
public class C {
    /** @ojb.field primarykey="true" autoincrement="ojb" */
    Integer id;
    /** @ojb.field length="30" */
    String name;
}


Ok, once we have that, problems start. First problem is that xdoclet does not like include-inherited="false", and when include-inherited="true", it generates all inherited attributes into repository.xml - but we don't want them there! After some patching of xdoclet [2], we can get desired result:


<class-descriptor class="A" table="A_TABLE">
  <extent-class class-ref="B"/>
  <field-descriptor name="id" column="id" jdbc-type="INTEGER"
                    primarykey="true" autoincrement="true"/>
  <field-descriptor name="cId" column="cId" jdbc-type="INTEGER"/>
  <reference-descriptor name="c" class-ref="C">
    <foreignkey field-ref="cId"/>
  </reference-descriptor>
</class-descriptor>

<class-descriptor class="B" table="B_TABLE">
  <field-descriptor name="id" column="id" jdbc-type="INTEGER"
                    primarykey="true" autoincrement="true"/>
  <field-descriptor name="aId" column="aId" jdbc-type="INTEGER"/>
  <field-descriptor name="value" column="value"
                    jdbc-type="VARCHAR" length="254"/>
  <reference-descriptor name="super" class-ref="A"
       auto-retrieve="true" auto-update="object" auto-delete="object">
    <foreignkey field-ref="aId"/>
  </reference-descriptor>
</class-descriptor>

<class-descriptor class="C" table="C_TABLE">
  <field-descriptor name="id" column="id" jdbc-type="INTEGER"
                    primarykey="true" autoincrement="true"/>
  <field-descriptor name="value" column="value"
                    jdbc-type="VARCHAR" length="30"/>
</class-descriptor>


Assuming that all of the above is correct, I'm getting following problems on run time:


1. First problem happens when trying to create instance of B (and assuming that there is a record for C) with the code:
B b = new B();
b.cId = 1;
b.value = "dummy";
broker.store(b);
This fails with KeyConstraintViolatedException. Seems it is happening because B's ClassDescriptor.getSuperClass() returns null and PersistentBrokerImpl.storeToDb does not store base class. So what's wrong, and can somebody clarify what's the difference between getBaseClass and getSuperClass methods? At least getBaseClass() is populated correctly.


2. Second problem happens when trying to load existing instance from the DB. Fails with OJBRuntimeException "Incorrect or not found field reference name 'cId' in descriptor <here comes A's CollectionDescriptor> for class-descriptor 'B'.
Seems like ObjectReferenceDescriptor does not take into account B's super class at all.


Any ideas what I'm doing wrong?

Thanks,
Vadim


[1] http://db.apache.org/ojb/docu/guides/advanced-technique.html#Mapping+Classes+on+Multiple+Joined+Tables
See class B after "Now let us consider a case where B_TABLE contains an additional foreign key column B_TABLE.A_ID referencing A_TABLE.ID".


[2] Patch to ModelConstraints:
@@ -238,7 +238,13 @@

for (Iterator it = elementClass.getExtentClasses(); it.hasNext();)
{
- queue.add(it.next());
+ ClassDescriptorDef extentClass = (ClassDescriptorDef) it.next();
+ // Exclude classes which belong to the "Mapping Classes on Multiple Joined Tables" scenario.
+ if (!extentClass.getBooleanProperty(PropertyHelper.OJB_PROPERTY_INCLUDE_INHERITED, true))
+ {
+ continue;
+ }
+ queue.add(extentClass);
}



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to