next up previous contents
Next: Many 2 Many Up: Storing in the Database Previous: Naming Conventions   Contents

Complex Storage and Heavy Lookups/Storage

What if the user were to have a reference to a Company object which represents the company that the user works for. The standard database method is to use a 'foreign-key'. Simply put, if the Company has an oid of n then a row in the user table for company_oid's would need to store n as this users value.

This is handled by the persistence layer for each object which has a foreign table the primary key (oid) is stored in an foreign_oid column as an int. HOWEVER, standard object lookups are light - meaning they don't retrieve the foreign objects. So simple retrieval of a User his company oid will be set to n but the Company object will be null.

To handle heavy lookups one must override a routine in the factory called deepRetrieval. The code in our UserFactory might look like this.

...
  protected void deepRetrieval (DatabaseSession dbSess,
                 Identified item) throws SQLException {
    User user = (User)item;
    user.setCompany (CompanyFactory.getInstance ().getByOid (dbSess,
            user.getCompanyOid ());
  }
...
Notice that we call CompanyFactory which you would have to write to extend the abstract factory and implement the singleton pattern. Also note that the only XML entry looked like.
  <field name="company"
      type="org.ephman.examples.user.generated.Company">
    <sql name="company_oid"/>
  </field>
And from this both a field for company and an int companyOid were placed in the Java file and only 'int company_oid' was created in schema.

For storage purposes there are two routines, deepStorage and preStorage. 'deepStorage' handles things like Vectors where each entry has an oid column referencing back to this object, and thus needs this object to be stored first. 'preStorage' is for Objects that the current one depends on: for a User to store his companies oid in the db, the Company object must be stored first, so the 'preStorage' routine allows you to save any objects which are referenced in a single-entry in this object.


next up previous contents
Next: Many 2 Many Up: Storing in the Database Previous: Naming Conventions   Contents
2002-03-05