![]() |
![]() |
Back to the Speedo tutorial home page
![]() |
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. |
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" |
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 |
/**
* 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();
}