This procedure describes how to prepare and adjust the application entities by changing table names, defining ID fields, adding relation handling fields, defining relation types, and so on.
To change the table name of the entities, open the JPA Details view by choosing
.Open for editing the Employee entity (class) , select Employee public class, and in the JPA Details view (under Table area) change the table name to EDM_EMPLOYEE.
Select the employeeId
variable
in the Employee class, and in the JPA Details view
do the following:
from the Map As dropdown menu, select ID.
change the name in the Column area to EMPLOYEE_ID
Select the Primary Key Generation and Table Generator checkboxes and enter the following:
Strategy : Table
Generator Name: IdGenerator
Name: IdGenerator
Table: EDM_GENERATOR
Primary Key Column: BEAN_NAME
Value Column: MAX_ID
Primary Key Column Value: Default
As a result, the toolset automatically generates the following annotation.
Syntax
@GeneratedValue(strategy = GenerationType.TABLE, generator = "IdGenerator") @TableGenerator(name = "IdGenerator", table = "EDM_GENERATOR", pkColumnName = "BEAN_NAME", valueColumnName = "MAX_ID")
In the Employee class source code, add a new field projectsByParticipant to handle a many to many relation between the entities. Enter the following source code:
private List<Project> projectsByParticipant
= new ArrayList<Project> ();
From the secondary mouse button, select
.Select the projectsByparticipant field and from the context menu select
.Select the projectsByparticipant field in the Outline view and from the JPA Details view (scroll to the very top), select the following details:
Map as: ManytoMany
Fetch: Lazy
Mapped by: employees
This generates the following annotation in the Employee class source code.
@ManyToMany(fetch=LAZY, mappedBy = "employees")
Select the version field and from the Map As dropdown menu of the JPA Details view map it as Version.
Now the entity can use optimistic locking as described in the JPA specification.
Under @Table(name="EDM_EMPLOYEE")
enter
the following NamedQueries annotation in the Employee class source code:
Syntax
@Table(name="EDM_EMPLOYEE") @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.getById", query = "SELECT e FROM Employee e WHERE e.employeeId = :id") })
To change the table name of the entities, choose
.Open for editing the Project entity , select Project public class, and in the JPA Details view (under Table area) change the table name to EDM_PROJECT.
Select the projectId
variable
in the Project class , and in the JPA Details view
do the following:
from the Map As dropdown menu, select ID
change the name in the Column area to PROJECT_ID
Select the Primary Key Generation and Table Generator checkboxes and enter the following:
Strategy : Table
Generator Name: IdGenerator
Name: IdGenerator
Table: EDM_GENERATOR
Primary Key Column: BEAN_NAME
Value Column: MAX_ID
Primary Key Column Value: Default
As a result, the toolset automatically generates the following annotation.
Syntax
@GeneratedValue(strategy = GenerationType.TABLE, generator = "IdGenerator") @TableGenerator(name = "IdGenerator", table = "EDM_GENERATOR", pkColumnName = "BEAN_NAME", valueColumnName = "MAX_ID")
In the Project class source code, add a new field employees to handle a many to many relation between the entities. Enter the following source code:
private List<Employee> employees
= new ArrayList<Employee>();
From the secondary mouse button select
.Select the employees field and from the context menu select
.Select the employees field in the Outline view and from the JPA Details view (scroll to the very top), select the following details:
Map as: ManytoMany
Fetch: Eager
Under the Join Table, enter name EDM_EMP_PRJ, select both Override Default indicators and do the following:
For the area Join Columns, choose Add, and enter PROJECT_ID for both Name and Referenced Column Name. Remove the default entry.
For the area Inverse Join Columns, choose Add, and enter EMPLOYEE_ID for both Name and Referenced Column Name. Remove the default entry.
The toolset automatically generates the following annotation.
Syntax
@ManyToMany(fetch=EAGER) @JoinTable( name = "EDM_EMP_PRJ", joinColumns = @JoinColumn( name = "PROJECT_ID", referencedColumnName = "PROJECT_ID"), inverseJoinColumns = @JoinColumn( name = "EMPLOYEE_ID", referencedColumnName = "EMPLOYEE_ID"))
Select the version field and from the Map As dropdown menu of the JPA Details view map it as Version.
Now the entity can use optimistic locking as described in the JPA specification.
Under @Table(name="EDM_PROJECT")
enter
the following NamedQueries annotation in the Project class source code:
Syntax
@Table(name="EDM_PROJECT") @NamedQueries( { @NamedQuery(name = "Project.getAll", query = "SELECT p FROM Project p"), @NamedQuery(name = "Project.findByNameOrDescPart", query = "SELECT p FROM Project p WHERE p.description LIKE :namepart OR p.title like :namepart1"), @NamedQuery(name = "Project.getByProjectID", query = "SELECT p FROM Project p WHERE p.projectId = :prjId") })
In the Outline view select the startDate field and from its context menu select
.Note
Ignore the error messages saying the Project and Employee tables do not exist. The errors will be resolved later on, when you generate the database tables.