Show TOC

Procedure documentationAdjusting the JPA Entities Locate this document in the navigation structure

 

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.

Adjusting the Employee Entity
  1. To change the table name of the entities, open the JPA Details view by choosing   Window   Show View   Other   JPA Development   JPA Details  .

  2. 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.

  3. 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

  4. 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 Syntax

      1. @GeneratedValue(strategy = GenerationType.TABLE, generator = "IdGenerator") 
        @TableGenerator(name = "IdGenerator", table = "EDM_GENERATOR",
         pkColumnName = "BEAN_NAME", valueColumnName = "MAX_ID")
      End of the code.
  5. 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> ();

  6. From the secondary mouse button, select   Source   Organize Imports   java.util.List   Finish  .

  7. Select the projectsByparticipant field and from the context menu select   Source   Generate Getters and Setters  .

  8. 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")

  9. 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.

  10. Under @Table(name="EDM_EMPLOYEE")enter the following NamedQueries annotation in the Employee class source code:

    Syntax Syntax

    1. @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")
      })
      
    End of the code.
Adjusting the Project Entity
  1. To change the table name of the entities, choose   Window   Show View   Other   JPA Development   JPA Details  .

  2. 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.

  3. 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

  4. 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 Syntax

      1. @GeneratedValue(strategy = GenerationType.TABLE, generator = "IdGenerator") 
        @TableGenerator(name = "IdGenerator", table = "EDM_GENERATOR",
         pkColumnName = "BEAN_NAME", valueColumnName = "MAX_ID")
      End of the code.
  5. 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>();

  6. From the secondary mouse button select   Source   Organize Imports   java.util.List   Finish  .

  7. Select the employees field and from the context menu select   Source   Generate Getters and Setters  .

  8. 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

  9. 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 Syntax

    1. @ManyToMany(fetch=EAGER)
         @JoinTable(
      	name = "EDM_EMP_PRJ", 
      	joinColumns = @JoinColumn(
      			name = "PROJECT_ID", 
      			referencedColumnName = "PROJECT_ID"), 
      	inverseJoinColumns = @JoinColumn(
      			name = "EMPLOYEE_ID", 
      			referencedColumnName = "EMPLOYEE_ID"))
      
    End of the code.
  10. 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.

  11. Under @Table(name="EDM_PROJECT")enter the following NamedQueries annotation in the Project class source code:

    Syntax Syntax

    1. @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") 
      
      })
    End of the code.
  12. In the Outline view select the startDate field and from its context menu select   Java EE Annotations   ORM   Temporal  .

    Note 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.

    End of the note.