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 gong <go...@cal.sony.co.jp> on 2003/12/02 09:21:19 UTC

summay of ojb 1:n auto-update, auto-delete and RemovalAwareCollection

about ojb 1:n auto-update, auto-delete
and RemovalAwareCollection
                            ----Spring G.H.
                                2003-12-02 

Thanks oliver.matz@ppi.de 
   and Thomas Mahler(thma@apache.org) 
for helping me resolve this problem.

any suggestion is appreciated!  

**********************************************
INTENTION: to help many Newbies as me how 
to resolve the auto-update, auto-delete 
problem about 1:n relationship when using ojb.
**********************************************
-
**********************************************
EXAMPLE: this is a general example which has
a 1:n relationship between myList and myEntry,
i.e. each myList has a collection of myEntry 
-
The way to mapping them into DB always is the 
same as OJB-TUTORIAL-3 described:
 USE "collection" in java code, AND
 USE "collection-descriptor" in reporsitory.xml
for the "n" object.
**********************************************
************* java code **********************
...
import org.apache.ojb.broker.util.collections.*;
...

Class myList
{
  ...
  List entries;
  ...

  entries = new RemovalAwareCollection();
  ...
}

Class myEntry
{
  String entryName;
  ...
}


***********************************************
given these two class, the general usecase is:

FIRST,  create a myList and populate myList.entries
        with serveral myEntry.
SECOND, store myList into DB. at this time, we 
        hope ojb automatically store myEntry
        into DB also.
THIRD,  retrieve myList, and add or remove some
        entry into/from myList.entries, then
        store myList back DB again. at this time
        we hope ojb automatically store the added
        entries and AUTOMATICALLY delete the
        entries from DB table which have
        been removed from myList.entries. 

that's all, it is the usecase.
***********************************************

store them in two DB tables as

CREATE TABLE list
(
  id  varchar(20) not null,
  ...

  primary key(id)
);


CREATE TABLE entry
(
  id         varchar(20) not null,
  list_id    varchar(20),
  name       varchar(100),
  ...

  primary key(id),
  foreign key(list_id) references list(id)   
);


in reporsitory.xml:

<!-- ====  List  ==== -->

<class-descriptor
      class="myList"
      table="list"
>
  <field-descriptor
     name="id"
     column="id"
     jdbc-type="VARCHAR"
     primarykey="true"
     autoincrement="true"
  />

  <collection-descriptor
      name="entries"
      element-class-ref="myEntry"
      auto-retrieve="true"
      auto-update="true"
      auto-delete="true"
      >
      <inverse-foreignkey field-ref="listId"/>
  </collection-descriptor>

</class-descriptor>


<!-- ====  Entry  ==== -->

<class-descriptor
      class="myEntry"
      table="entry"
>
  <field-descriptor
     name="id"
     column="id"
     jdbc-type="VARCHAR"
     primarykey="true"
     autoincrement="true"
  />
  <field-descriptor
     name="listId"
     column="list_id"
     jdbc-type="VARCHAR"
     access="anonymous"
  />
  <field-descriptor
     name="name"
     column="name"
     jdbc-type="VARCHAR"
  />
</class-descriptor>

*****************************************
in this way, ojb can automatically update
myList, and automatically delete entries
from db which have been removed from 
myList.entries.
*****************************************

*****************************************
*************** IMPORTANT ******************
*****************************************

FIRST, u must use RemovalAwareCollection 
       in your java code, otherwise it
       can not work.

SECOND, u SHOULD describe the collection
        in reporsitory.xml like this:

           <collection-descriptor
               name="entries"
               element-class-ref="myEntry"
               auto-retrieve="true"
               auto-update="true"
               auto-delete="true"
               >
           <inverse-foreignkey field-ref="listId"/>
           </collection-descriptor>

        its default collection-class is
        "RemovalAwareCollection",

        if you describe collection like below, it can
        not work. please pay attention to this.

          <collection-descriptor
              name="entries"
         **** collection-class="org.apache.ojb.broker.util.collections.

ManageableArrayList" *****
              element-class-ref="myEntry"
              auto-retrieve="true"
              auto-update="true"
              auto-delete="true"
              >
              <inverse-foreignkey field-ref="listId"/>
          </collection-descriptor>




 


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