Show TOC Start of Content Area

Procedure documentation Implementing the Employee Entity  Locate the document in its SAP Library structure

Use

This procedure shows you how to implement the entities you generated in the previous step of the tutorial.

The automatic generation detects all columns of the tables and generates persistent fields in the entity accordingly. In this case, the CERA_EMPLOYEE table contains a column DEPARTMENT_ID, which is a foreign key in a one-to-many relationship between the CeraDepartment and CeraEmployee entities. This means that the CeraEmployee entity should not contain a persistent entity mapped to this column.

For each relationship the Employee entity participates in, you must define new variables that hold the foreign keys of the inverse side of the relationships.

Procedure

1. Adjusting the Persistent Fields

...

       1.      Open the entity for editing.

       2.      To open the JPA Development perspective, navigate to Window Open Perspective Other JPA Development and choose OK.

The JPA Structure view displays all persistent fields this entity has.

This graphic is explained in the accompanying text

       3.      Delete the departmentId field and the corresponding getter and setter methods using the source code editor.

       4.      Add the following new variables:

private List<CeraProject> projectsLeadByEmployee = new ArrayList<CeraProject>();

 

private List<CeraProject> projectsByParticipant = new ArrayList<CeraProject>();

 

private List<CeraSkill> skills = new ArrayList<CeraSkill>();

 

private List<CeraUsergroup> groups = new ArrayList<CeraUsergroup>();

 

private CeraDepartment employeeDepartment;

 

private CeraDepartment deptManager;

 

       5.      Import the necessary classes by choosing SourceOrganize Imports from the context menu.

Make sure you select the java.util.List from the proposed options.

Note

At this point you get error messages for the employeeDepartment and the deptManager entries. To overcome these issues we recommend you copy the source codes of all entities from the section Reference. The error messages are handled later on when you set relationships between the entities.

       6.      To generate getter and setter methods for the new fields, choose Source Generate Getters and Setter.

Select all variables in the list except serialVersionUID, and choose OK.

       7.      Add the following two methods that are used to manage an employee’s set of skills:

public void addSkill(CeraSkill s) {

  this.getSkills().add(s);

}

public void removeSkill(CeraSkill s) {

  this.getSkills().remove(s);

}

2. Defining ID Generation

       1.      Select the employeeId field from the JPA Structure view.

This field is already marked as one that holds the primary key of the Employee entity based on the settings of the CERA_EMPLOYEE table.

       2.      Open the JPA Details view by choosing Window Show View Other JPA Development JPA Details.

       3.      To define the primary key generation strategy, open the Primary Key  Generation tab and proceed as follows:

                            a.      Select the Primary Key Generation indicator.

                            b.      Select Table from the Strategy dropdown list.

                            c.      Enter IdGenerator in the Generator Name field.

                            d.      Expand the Table Generator list and select the Table Generator checkbox.

                            e.      Enter CERA_GENERATOR in the Table field.

                              f.      Select BEAN_NAME from the Primary Key Column.

                            g.      Select MAX_ID from the Value column.

3. Defining Relationships

...

       1.      Define a one-to-one relationship between the Employee and Department entities, in which Employee is the owning side of the relationship:

                            a.      Select the deptManager field from the JPA structure view.

                            b.      Provide the following information in the JPA Details view.

Property

Value

Map As

One to One

Fetch

Lazy

Mapped By

manager

This generates the following annotation for the deptManager field:

@OneToOne(mappedBy="manager", fetch = LAZY)

       2.      Define a many-to-one relationship between the Employee and the Department entities for the employeeDepartment variable. Complete the following steps:

                            a.      Select the employeeDepartment field from the JPA structure view.

                            b.      In JPA Details view, enter the following data:

Property

Value

Map As

Many to one

Fetch

Lazy

                            c.      On the Join Columns tab page, enable the Override Default indicator, then select the entry under Join Columns field, and choose Edit. Select DEPARTMENT_ID from the Name dropdown list, leave default(DEPARTMENT_ID) for the Referenced Column Name, and choose OK.

This generates the following code for the employeeDepartment field:

@ManyToOne(fetch=LAZY)

@JoinColumn(name="DEPARTMENT_ID")

private CeraDepartment employeeDepartment;

       3.      Using the same procedure from step 2 you can define the other relationships that Employee participates in. To input in the respective fields in the JPA Details view, use the tables below for the corresponding fields:

¡        Persistent field groups

Property

Value

Map As

Many to many

Fetch

Lazy

Tab Page: Join Table

Name

CERA_EMP_UGR

Join Columns

Name: EMPLOYEE_ID

Referenced Column Name: EMPLOYEE_ID

Inverse Join Columns

Name: GROUP_ID

Referenced Column Name: GROUP_ID

Generated Code

@ManyToMany(fetch=LAZY)

@JoinTable(name="CERA_EMP_UGR", joinColumns = @JoinColumn(name = "EMPLOYEE_ID", referencedColumnName = "EMPLOYEE_ID"), inverseJoinColumns = @JoinColumn(name = "GROUP_ID", referencedColumnName = "GROUP_ID"))

private List<CeraUsergroup> groups = new ArrayList<CeraUsergroup>();

       Persistent field skills

Property

Value

Map As

Many to many

Fetch

Eager

Tab Page: Join Table

Name

CERA_EMP_SKILL

Join Columns

Name: EMPLOYEE_ID

Referenced Column Name: EMPLOYEE_ID

Inverse Join Columns

Name: SKILL_ID

Referenced Column Name: SKILL_ID

Generated Code

@ManyToMany(fetch=EAGER)

@JoinTable(name="CERA_EMP_SKILL", joinColumns = @JoinColumn(name = "EMPLOYEE_ID", referencedColumnName = "EMPLOYEE_ID"), inverseJoinColumns = @JoinColumn(name = "SKILL_ID", referencedColumnName = "SKILL_ID"))

private List<CeraSkill> skills = new ArrayList<CeraSkill>();

       Persistent field projectsLeadByEmployee

Property

Value

Map As

One to many

Fetch

Lazy

Mapped By

leader

Generated Code

@OneToMany(fetch=LAZY, mappedBy = "leader")

private List<CeraProject> projectsLeadByEmployee = new ArrayList<CeraProject>();

       Persistent field projectsByParticipant

Property

Value

Map As

Many to many

Fetch

Lazy

Mapped By

employees

Generated Code

@ManyToMany(fetch=LAZY, mappedBy = "employees")

private List<CeraProject> projectsByParticipant = new ArrayList<CeraProject>();

 

 

4. Defining Named Queries

       1.      Open the Outline view (Window Show view Outline from the main menu) and select CeraEmployee.

       2.      Choose Java EE Annotations Entity NamedQueries from the context menu for CeraEmployee.

This generates the @NamedQueries annotation for the CeraEmployee class.

This graphic is explained in the accompanying text

       3.      Define the following queries using the table below.

Name

Query

Employee.findByNamePart

SELECT e FROM Employee e WHERE lower(e.lastName) LIKE :namepart OR lower(e.firstName) LIKE :namepart1

Employee.findByLogin

SELECT e FROM Employee e WHERE e.login = :login

Employee.getAll

SELECT e FROM Employee e

Below you can find the resulting annotations that appear in your code:

@NamedQueries({

   @NamedQuery(name="Employee.getAll", query="SELECT e FROM Employee e"),

   @NamedQuery(name="Employee.findByNamePart", query="SELECT e FROM Employee e WHERE e.lastName LIKE :namepart OR e.firstName LIKE :namepart1"),

   @NamedQuery(name="Employee.findByLogin", query="SELECT e FROM Employee e WHERE e.login = :login")

})

 

End of Content Area