Entering content frame

Procedure documentation Defining the Persistence Capable Classes Locate the document in its SAP Library structure

Use

Persistence capable classes define objects that can be stored in and retrieved from the persistent data store.

We are now going to create the following persistence capable classes:

·        Employee – represents the employees of a company

·        Department – represents the departments of the company.

 

Prerequisites

You must have created a Web Project and set its build path.

 

Procedure

The persistent fields in the Employee and the Department classes are declared as private variables. To read and write data in the fields, use the getXXX() and setXXX() method model in the same way as they are used in the JavaBeans model.

The relationship between the Employee and the Department instances is many-to-one – that is, each employee can work in a single department, although a department may comprise an arbitrary number of employees. This relationship is implemented by the field Department in the Employee class, and the field Employees in the Department class.

Both the Employee and Department instances are identified by a unique numeric identifier. It is set in the constructor of the class, and can only be read using a get() method, but you cannot change it in a set() method.

Furthermore, the JDO standard requires that each persistence capable class has a no-args constructor.

Creating the Employee Class

       1.      In the J2EE Development perspective, open the context menu of your Web project.

       2.      Choose New ®Package. Enter temp.persistence.gettingstarted.jdo as the package name and choose Finish.

       3.      Choose New ® Java Class. Enter Employeeas the class name. To choose a package, choose Browse… and select temp.persistence.gettingstarted.jdo. To create the class, choose Finish.

       4.      The new Java file opens automatically. You can enter the code for the Employee class.

Each employee is defined by the following attributes:

¡        First name

¡        Last name

¡        Salary

¡        Numeric identifier (employee ID)

You must include an import declaration for the class java.math.BigDecimal, because the variable salary is of type BigDecimal.

The Employee class then looks as follows:

Example

package temp.persistence.gettingstarted.jdo;

 

import java.math.BigDecimal;

 

public class Employee {

  

   // Class attributes: the persistent fields of an employee.

   // Also defined inside the file Employee.jdo

   private int empId;

   private String firstName;

   private String lastName;

   private BigDecimal salary;

   private Department department;

 

   // Required: a no-args constructor

   public Employee() {

      this.empId = -1;

      this.firstName = "INITIAL";

      this.lastName = "INITIAL";

      salary = new BigDecimal(0.0);

      department = null;

   }

 

   // Constructor where the ID is set

   public Employee(int empId) {

      this.empId = empId;

      this.firstName = "INITIAL";

      this.lastName = "INITIAL";

      salary = new BigDecimal(0.0);

      department = null;

   }

 

   // Implement the getter methods of the class

   public Department getDepartment() {

      return department;

   }

 

   public int getEmpId() {

      return empId;

   }

 

   public String getFirstName() {

      return firstName;

   }

 

   public String getLastName() {

      return lastName;

   }

 

   public BigDecimal getSalary() {

      return salary;

   }

 

   // Implement the setter methods of the class

   public void setDepartment(Department dept) {

   this.department = dept;

   }

 

   public void setFirstName(String fname) {

      firstName = fname;

   }

 

   public void setLastName(String lname) {

      lastName = lname;

   }

 

   public void setSalary(BigDecimal sal) {

      salary = sal;

   }

}

 

Creating the Department Class

...

       1.      Use the procedure described above to create a new class. At step 2, enter Department as the class name.

       2.      Enter the code for the Department class.

The department instances are defined by the following attributes:

¡        Name

¡        Numeric identifier (Department ID)

You have to import the java.util.* package because the type of the employees field is java.util.Set, and the getEmployees() method returns a java.util.Collection. In addition, you have to implement logic for adding and removing employees in the department, and you can use java.util.HashSet to organize the addition of employees.

The Department class then looks as follows:

Example

package temp.persistence.gettingstarted.jdo;

 

import java.util.*;

 

public class Department {

  

   // Class attributes: the persistent fields of a department.

   // Also defined inside the file Department.jdo

   private int depId;

   private String name;

   private Set employees;

  

   // Required: a no-args constructor

   public Department() {

      this.depId = -1;

      name = "INITIAL";

      employees = new HashSet();

   }

  

   // Constructor where the ID is set

   public Department(int depId) {

      this.depId = depId;

      name = "INITIAL";

      employees = new HashSet();

   }

  

   // Implement the getter methods

   public int getDepId() {

      return depId;

   }

 

   public Collection getEmployees() {

      return employees;

   }

 

   public String getName() {

      return name;

   }

 

   // Implement the setter methods

   public void setName(String newName) {

      name = newName;

   }

 

   public void setEmployees(Set empSet) {

      this.employees = empSet;

   }

 

   // Assign employees to the department

   public void addEmployee(Employee emp) {

      if (employees == null)

      employees = new HashSet();

      employees.add(emp);

   }

 

   // Remove employees from the department

   public void removeEmployee(Employee emp) {

      if (employees != null)

      employees.remove(emp);

   }

}

 

Result

You have implemented the persistence capable classes. Continue by adding the object identity classes.

 

 

Leaving content frame