You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Oliver <ol...@gmx.net> on 2004/06/22 20:33:21 UTC

2nd try: Subtle mapping on multiple joined tables

Hello,
we would be very grateful for suggestions:

Environment: OJB 1.0rc7 with PostgreSql
We read many other postings and the docs but still have problems with the
following:

In a nutshell:
B is abstract
C extends B
A holds references to objects of type B (C-objects) Every C has reference to
"his" A. 

Strategy is: Mapping Classes on Multiple Joined Tables I read that we cant
use ODMG because auto-update has to be true.
Is this still correct? - So we switched to PB for testing.

Tables:
A_TABLE with aID,
B_TABLE with bID and aID(foreignkey to A_TABLE), C_TABLE with cID and
bID(foreignkey to B_TABLE)

First problem and question:
OJB needs a reference from C to A, which is defined in class-descriptor for
B and cant be defined in C, because there is no column with a foreignkey to
A in C_TABLE.
Is that right? How can we solve this?

Due to this fact we added a column to C_TABLE: aID(foreignkey to A_TABLE)
for further testing.
Next problem:
If we create an A-object and insert a reference to a C-object into A's
collection, the attributes of A are stored, but C's not (attributes should
be stored in B_TABLE and C_TABLE), therefore we wrote auto-update="true"
auto-retrieve="true"
auto-delete="false"
into A's collection descriptor and we get:
java.lang.InstantiationException and
[org.apache.ojb.broker.core.PersistenceBrokerImpl] ERROR: Broker is still in
PB-transaction, do automatic abort before close!



Now the practical background for this problem is, how to map the "Role
Object Pattern" with OJB

For better understanding:
http://www.exa.unicen.edu.ar/catedras/faya2000/rop.pdf

PersonCore has a collection with objects of type PersonRole. There exist
some different roles, that extend PersonRole. It also should be possible to
continue recursively (e.g. Class Employee extends PersonRole, then
EmployeeCore and EmployeeRole extend Employee and EmployeeCore holds a
collection with objects of type EmployeeRole) PersonRole, Employee and
EmployeeRole are abstract.

This hirarchy should be mapped with the "Mapping Classes on Multiple Joined
Tables" strategy:

PERSONCORE
-------------------
personId PRIMARY KEY

PERSONROLE
-------------------
roleId PRIMARY KEY
personId FOREIGN KEY to PERSONCORE

ADMIN
---------
adminId PRIMARY KEY
roleId FOREIGN KEY to PERSONROLE
(personId added for testing - see above - how can we avoid this key??):
personId FOREIGN KEY to PERSONCORE

further tables: EMPLOYEE, CHIEF etc...

In the class descriptor for PersonCore we have the collection- descriptor
for a vector, which holds the references to PersonRoles (PersonRole is
abstract!):
<collection-descriptor
         name="vRoles"
         element-class-ref="PersonRole"
         auto-update="true"
         auto-retrieve="true"
         auto-delete="false">
  <inverse-foreignkey field-ref="personId"/> </collection-descriptor>


The class-descriptor for the abstract class PersonRole:
The description attribute gets inherited to extending classes and there is
also a reference descriptor for the "backward" reference to PersonCore

<class-descriptor  class="PersonRole"  table="personrole">
  
  <extent-class class-ref="Admin" /> 
    ..........other extents follow..........

  <field-descriptor 
    name="personRoleId"
    column="personroleid"
    jdbc-type="INTEGER"
    primarykey="true"
    autoincrement="true"
    sequence-name="personroleid_personroleid_seq"
    access="anonymous"
  />  	
  		
  <field-descriptor 
    name="description"
    column="description"
    jdbc-type="VARCHAR"
  />
  
  <field-descriptor 
    name="personId"
    column="personid"
    jdbc-type="VARCHAR"
    access="anonymous"
  />

  <reference-descriptor
      name="personCore"
      class-ref="PersonCore">
         <foreignkey field-ref="personId"/>
   </reference-descriptor>
</class-descriptor>


Mapping for the concrete role "Admin":

<class-descriptor class="Admin" table="admin">
  <field-descriptor id="0"
    name="adminId"
    column="adminid"
    jdbc-type="INTEGER"
    primarykey="true"
    autoincrement="true"
    sequence-name="admin_adminid_seq"
    access="anonymous"
  />
  <field-descriptor
     name="personRoleId"
     column="personroleid"
     jdbc-type="INTEGER"
     access="anonymous"
  />
       
  <reference-descriptor name="super" class-ref="PersonRole"
        auto-retrieve="true"
        auto-update="true"
        auto-delete="true">
        <foreignkey field-ref="personRoleId"/>
  </reference-descriptor>
</class-descriptor>


The Admin table has no column with a key to Person by default, but
as emntioned above we need the reference descriptor for pointing to
PersonCore in the Admin class-descriptor also?
Added for testing (how to avoid this?):
<field-descriptor name="personId" column="personid" jdbc-type="VARCHAR"
    access="anonymous"/>

<reference-descriptor name="personCore" class-ref="PersonCore">
         <foreignkey field-ref="personId"/>
</reference-descriptor>


We are looking forward to get some recommendations and solutions for
mapping this class hierarchy with ojb using the 
"Mapping Classes on Multiple Joined Tables" strategy

Greetings
Oliver


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


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