Speedo Tutorial - Mapping

 FinalLogo.jpg
 FinalLogo.jpg
  1. Object model
  2. Step 3 : mapping
  3. Step 3: make objects persistent

Back to the Speedo tutorial home page




Object model

   The third step is based on the following object model:
   

A Manager is identified by his name, is the head-of a department (1:1 relationship), and supervises many employees (1:n relationship). A Department is identified by its label and managed by on manager. An Employee is identified by his name, supervised by one manager and involved into many projects (m:n relationship). A project is identified by its title and managed by many employees.

The source code of the java classes is located in the
org.objectweb.speedo.tutorial.pobjects.mapping package.

    Top

Step 3 : mapping

    The aim of this step is to edit the jdo file defining the mapping of the object model described above.

<!DOCTYPE jdo PUBLIC "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN"
"jdo.dtd">
<jdo> 1
<package name="org.objectweb.speedo.tutorial.pobjects.mapping"> 2

<class name="Manager" identity-type="application"> 3
<extension vendor-name="speedo" key="sql-name" value="T_MAPPING_MANAGER"/> 4
<field name="name" primary-key="true"> 5
<extension vendor-name="speedo" key="sql-name" value="NAME"/> 6
</field>
<field name="department" persistence-modifier="persistent"> 7
<!-- 1-1 relationship -->
<extension vendor-name="speedo" key="target-foreign-keys" value="LABEL=DPT_LABEL"/>
<extension vendor-name="speedo" key="reverse-field" value="manager"/>
</field>
<field name="employees" persistence-modifier="persistent"> 8
<!-- 1-N relationship -->
<collection element-type="Employee"/>
<extension vendor-name="speedo" key="source-foreign-keys" value="NAME=MANAGER_NAME"/>
<extension vendor-name="speedo" key="reverse-field" value="manager"/>
</field>
</class>

<class name="Department" identity-type="application">
<extension vendor-name="speedo" key="sql-name" value="T_MAPPING_DEPARTMENT"/>
<field name="label" primary-key="true">
<extension vendor-name="speedo" key="sql-name" value="LABEL"/>
</field>
<field name="manager" persistence-modifier="persistent">
<!-- 1-1 relationship already defined above (see 7)-->
</field>
</class>

<class name="Employee" identity-type="application">
<extension vendor-name="speedo" key="sql-name" value="T_MAPPING_EMPLOYEE"/>
<field name="name" primary-key="true">
<extension vendor-name="speedo" key="sql-name" value="NAME"/>
</field>
<field name="manager" persistence-modifier="persistent">
<!-- 1-N relationship already defined above (see 8)-->
</field>
<field name="projects" persistence-modifier="persistent"> 9
<!-- M-N relationship -->
<collection element-type="Project"/>
<extension vendor-name="speedo" key="join-table" value="EMPLOYEE_TO_PROJECT"/>
<extension vendor-name="speedo" key="source-foreign-keys" value="NAME=EMPLOYEE_NAME"/>
<extension vendor-name="speedo" key="target-foreign-keys" value="TITLE=PROJECT_TITLE"/>
<extension vendor-name="speedo" key="reverse-field" value="employees"/>
</field>
</class>

<class name="Project" identity-type="application">
<extension vendor-name="speedo" key="sql-name" value="T_MAPPING_PROJECT"/>
<field name="title" primary-key="true">
<extension vendor-name="speedo" key="sql-name" value="TITLE"/>
</field>
<field name="employees" persistence-modifier="persistent">
<!-- M-N relationship already defined above (see 9)-->
<collection element-type="Employee"/>
</field>
</class>

</package>
</jdo>
1 the jdo tag to specify it is a jdo descriptor

2 define the package described by this descriptor

3  define the class name and the identity type
    (datastore or application)       

4  define the name of the rdb table
    (by default the table name is equal to the name of the class)
           
5  define the field as primary key
           
6 define the sql column name associated to this field
             
7 1-1 relationship:
    a- the persistent-modifier is set to persistent
    b- define the target-foreign-keys with value LABEL:DPT_LABEL
       where LABEL is the column name (PK) of the T_MAPPING_DEPARTMENT table
       and DPT_LABEL the column name (FK) of the  T_MAPPING_MANAGER table
    c- specify the reverse field:
       manager is the name of the java field in the Department java class
       which is a reference to a Manager instance.
          
8 1-N relationship:
    a- the persistent-modifier is set to persistent
    b- define the element-type of the collection: Employee is the name of the java class
    c- define the source-foreign-keys with value NAME:MANAGER_NAME
       where NAME is the column name (PK) of the T_MAPPING_MANAGER table
       and MANAGER_NAME the column name (FK) of the  T_MAPPING_EMPLOYEE
    d- specify the reverse field:
       manager is the name of the java field in the Employee java class
       which is a reference to a Manager instance   
          
9
M-N relationship:
    a- the persistent-modifier is set to persistent
    b- define the element-type of the collection: Project is the name of the java class
    c- define a join table, its name is EMPLOYEE_TO_PROJECT
    d- define the source-foreign-keys with value NAME:EMPLOYEE_NAME
       where NAME is the column name (PK) of the T_MAPPING_EMPLOYEE table
       and EMPLOYEE_NAME the column name (FK) of the T_MAPPING_PROJECT
    e- define also the target-foreign-keys with value
       TITLE:PROJECT_TITLE where TITLE is the column name (PK) of the T_MAPPING_PROJECT table
       and PROJECT_TITLE the column name (FK) of the T_MAPPING_EMPLOYEE
    f- specify the reverse field:
       employees is the name of the java field in the Project java class
       which is a reference to Employee instances     

    More information about the mapping (speedo documentation).
   
    Top


Step 3: make objects persistent

    To illustrate this mapping we create objects and make them persistent.
      

/**
* This step enables to check the results of the mapping on the database:
* objects are made persistent, then you can view the results on the datastore.
*/
public static void mapping() {
System.out.println( "***************Mapping*****************");
//create a department
Department department = new Department("Sales");
//create a manager
Manager manager = new Manager("Young Emy", department);
//link the manager to the department
department.setManager(manager);
//create employees
Employee employee1 = new Employee("Truffaz Brad", manager);
Employee employee2 = new Employee("Serio Laura", manager);
Employee employee3 = new Employee("Burley Keith", manager);
Employee employee4 = new Employee("Stern Jan", manager);
//create projects
Project project1 = new Project("Iris");
Project project2 = new Project("Platine");
//assign projects to employees
employee1.addProject(project1);
employee2.addProject(project1);
employee3.addProject(project1);
employee1.addProject(project2);
employee4.addProject(project2);

//get the pm
PersistenceManager pm = pmf.getPersistenceManager();
//begin a transaction
pm.currentTransaction().begin();
//make persistent the two projects
// all the references reachable from these objects will be made persistent
System.out.println( "make persistent the project1 " + project1.toString());
pm.makePersistent(project1);
System.out.println( "make persistent the project2 " + project2.toString());
pm.makePersistent(project2);
//commit the transaction
pm.currentTransaction().commit();
//close the pm
pm.close();
}

  To launch the step 3:
    You can view the results of the mapping method in your database via the DatabaseManager (see Tutorial Environment): tables T_MAPPING_MANAGER,                                                         T_MAPPING_DEPARTMENT, T_MAPPING_EMPLOYEE, T_MAPPING_PROJECT and the join table EMPLOYEE_TO_PROJECT.

    Top

Step BackFollowing step