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

Simple storage

Okay, so we have a class User but now we want to get it stored in the database. The first step for all our database classes is to add a field called 'oid' of type integer (required by Abra) and specify that it is the identity column.

This would look something like this.

<!-- property nodes can be used to define common variables like
packages and regular expressions -->
  <property name="user.pkg" value="org.ephman.examples.user.generated"/>

  <class name="${user.pkg}.User"
          identity="oid">
    <description>This class represents a User</description>
    <field name="oid" type="integer"/>
    ...
  </class>
This piece of code issues the directive that the primary key will be 'oid'(identity statement).

Next we need to describe the database schema of the class User. This consists of two parts: putting a map-to element inside the class, which gives the name of the table that user should be stored in and assigning column names to each field using the sql element.

  <class name="${user.pkg}.User"
          identity="oid">
    <description>This class represents a User</description>
    <map-to table="example_user"/>

    <field name="oid" type="integer">
      <sql name="user_oid"/>
    </field>
    <field name="userId" type="string" len="32">
      <sql name="user_id"/>
    </field>
    <field name="userName" type="string" len="80">
      <sql name="user_name"/>
    </field>
  </class>
This defines the database table to be example_user and maps each field to their column(so the Java field userName maps to USER_NAME in the DB). left]this will also affect later validation Notice that the 'len' attribute has been added to specify how long each string should be.



2002-03-05