Show TOC Start of Content Area

Procedure documentation Creating Entities and Generating Database Tables  Locate the document in its SAP Library structure

Use

This procedure describes how to create entities manually and use the automatic generation of database tables function (also called forward mapping), which is provided by the SAP NetWeaver AS Java.

The generation of database tables from entities depends on how the object/relational mapping of the entities is organized. The object/relational mapping process includes mapping the entity to a table, specifying an entity field as primary key, associating entity fields to table columns, defining entities’ relationships, inheritance and so on.

Once you have mapped the entity to the relational database, you can use either the Java Dictionary or a physical database for the generation of database tables from entities.

Caution

If you are using the Java Dictionary for the forward mapping, you have to consider the following requirements when specifying the object/relational mapping:

       The table names must be in upper case.

       A prefix of the table name is required, for example, TMP_EMPLOYEE.

       The table names’ length must not exceed 18 characters.

Note

When you use a native database for the forward mapping, the SAP NetWeaver Developer Studio generates an SQL script based on the object/relational mapping of the entities. The actual process of database table generation is the SQL script execution.

Prerequisites

You have:

      Opened the JPA Development perspective

      Connected your project to the Java Dictionary or to a native database

More information:

Creating Connections to the Java Dictionary

Creating Database Connections

Procedure

...

       1.      Create the entity class.

To define a java class as entity, you simply add the @Entity annotation to the source code of the class.

...

                            a.      In the Project Explorer, select the project you want to use for development of entities.

                            b.      In the context menu, choose New Class.

                            c.      Enter package name in the Package field and class name in the Name field.

                            d.      (Optional) Specify the additional class properties.

                            e.      Choose Finish.

                              f.      Select the file of the class in the Package Explorer, for example Employee.java, and expand the node.

                            g.      In the context menu of the class, choose Open.

                            h.      In the JPA Details view, select Map As Entity from the dropdown list.

                              i.      Add fields to the entity class in the source code.

Example

@Entity

public class Employee {

  private int Id;

  private String firstName;

  private String lastName;

  private String Address;

}

       2.      Specify the object/relational mapping of the entity.

For more information about defining object/relational mapping, refer to the Dali Java Persistence Tools User Guide, provided by the SAP NetWeaver Developer Studio help content.

Note

Instead of specifying the object/relational mapping of the entities’ primary keys explicitly, you can also generate primary keys automatically.

More information: Generating Primary Keys

       3.      Generate a database table from the entity.

Note

If you want to generate several tables, you can create the corresponding entities and implement this procedure only once.

       Generating a Java Dictionary table

...

                                                  i.       In the Project Explorer, select the project you are using for development of entities.

                                                ii.       In the context menu, choose JPA Tools Generate DDL.

                                               iii.       Choose OK.

       Generating a database table

...

                                                  i.       In the Project Explorer, select the project you are using for development of entities.

                                                ii.       In the context menu, choose JPA Tools Generate DDL.

                                               iii.       Specify the project folder that contains the entities and a name for the sql file.

                                               iv.       Choose Next.

The generated SQL script file opens.

                                                 v.       In the context menu of the SQL code, choose Execute All.

The actual forward mapping is completed. To make the generated tables appear under the relevant database node in the Data Source Explorer, you have to refresh it and to clean the project from residual errors within the Java files.

                                               vi.       In the Data Source Explorer, select the database

                                              vii.       In the context menu, choose Refresh.

                                            viii.       To trigger a clean build of your project, choose Project Clean in the main menu, then choose OK.

End of Content Area