Show TOC

Using JPA in the Persistence LayerLocate this document in the navigation structure

Use

The persistence layer of enterprise applications deals with persistent data stored in a relational database. With Java EE 5, a new object-relational persistence API, the Java Persistence API (JPA) has been added to the platform. JPA leverages the best ideas from the existing persistence frameworks (such as JDO, Hibernate, Toplink and others), and finally provides a lightweight and easy-to-use persistence API integrated into any Java-EE-5-compliant application server.

In JPA, entities are plain old Java objects (POJOs) that have mapping to a relational database. Therefore you have to declare an entity explicitly to distinguish it from other regular Java objects that you might use within your application. You can use Java annotations or XML as a way of defining how the entity maps to your existing database table or tables.

The JPA framework introduces a central component, called the “entity manager” , which manages entity instances. As long as the entity manager controls the entity, you can expect that changes to the entity are synchronized with the database.

By means of the entity manager, you can access the Java Persistence API within other application layers, that is, the business logic (EJB 3.0) and the Web layer (servlets, JSF). JPA is also available in Web Dynpro and standalone.

To develop an application that uses JPA, no tool support is required. However, creating, modifying, and mapping entity classes at source level can become tedious and error-prone. To facilitate these tasks, the SAP NetWeaver Developer Studio includes the Web Tools Platform (WTP) project with the Dali JPA Tools subproject, version 1.0.

Working with Dali is described in the Dali Java Persistence Tools User Guide , which is provided by the SAP NetWeaver Developer Studio help content. The section below focuses on the specifics of using Dali with the SAP implementation of JPA 1.0.

For more information about JPA, see Overview of the Java Persistence API .

Procedure

This is the typical procedure for working with the JPA toolset Dali in the SAP NetWeaver Developer Studio:

1. Prepare for the Development of Entities

Two preparation steps are required:

  1. Create a database connection.

    You usually associate a project with a database connection. This may be a connection either to the Java Dictionary or to a native database. In this case, the Dali toolset validates the object-relational mapping of the entities against a database schema or creates database tables within the database schema automatically.

    Note

    If you want to use a database schema to validate your entities' object-relational mapping, you need a connection to a database.

    For more information, see:

    Creating Connections to the Java Dictionary

    Creating Database Connections

  2. Create a project with JPA support to contain the entities.

    To enable JPA support, you need a project with the Java Persistence facet. Depending on how you intend to package your application, the following options are possible:

    • Create a new project.

      • Create a new JPA project.

        Use this option if you want to develop your entities in a library module separately from the other application modules. In this case you do not need to add a Java Persistence facet to the project explicitly.

        For more information, see Creating JPA Projects in the Developer Studio .

      • Create a new EJB 3.0 project.

        In this case, you have to enable the Java Persistence facet explicitly.

        For more information, see Creating EJB Projects in the Developer Studio .

      • Create a new Dynamic Web project.

        In this case, you have to enable the Java Persistence facet explicitly.

        For more information, see Creating Dynamic Web Projects .

    • Use an existing project.

      To enable JPA support for an existing project, you have to add the Java Persistence facet to that project.

      For more information about adding JPA support to existing projects and configuring the Java Persistence facet, see Creating JPA Projects in the Developer Studio .

2. Create Entities and Define the Object-Relational Mapping

The persistence layer of enterprise applications serves as an intermediary between the business functions of the application and the data it stores in a relational database. This function of the persistence layer is also known as object-relational mapping because it maps Java objects to relational data.

JPA entities keep their persistent state in attributes . These are either fields or properties (getter and setter methods). Using annotations or XML, you can define the object-relational mapping between attributes of the entities and columns of the database tables.

There are three possible scenarios for implementing the entities and their object-relational mapping:

  • Create entities manually and generate database tables.

    Using this approach is suitable for developers who are comfortable with object-oriented programming rather than database programming. You can create new Java classes and annotate them as entities. You can also annotate existing Java classes as entities.

    For more information, see Creating Entities and Generating Database Tables .

  • Create database tables manually and generate entities.

    Using this approach requires more in-depth knowledge of database programming since it assumes the database model is completely defined (with all relationships, constraints and other aspects). You can create database tables using either the Java Dictionary or a physical database, depending on whether you want to use the system database or a different one.

    For more information, see Creating Database Tables and Generating Entities .

  • Map existing entities to existing database tables.

    If you have already manually created your entities and the relevant database tables, you have to follow the rules for object-relational mapping, described in the Dali Java Persistence Tools User Guide , which is provided by the SAP NetWeaver Developer Studio help content.

Create your Data Model with the JPA Diagram Editor

The SAP NetWeaver Developer Studio provides a JPA diagram editor tool that allows you to create, edit, and display the application data model (entities and their relationships) in a graphical environment.

For more information, see Creating Data Models with the JPA Diagram Editor .

3. Use the Java Persistence API in Other Application Components

In the other layers (business logic and user interfaces) of the same Java EE 5 application or external applications, you can use the entity manager to access the persistence functionality in the Java Persistence API from Enterprise JavaBeans (EJB 3.0), servlets and JavaServer Faces (JSF).

For more information about the entity manager, see Working with the Entity Manager .

For more information about using JPA in the business layer, see Using Java Persistence API (JPA) .

For more information about using JPA in Web layer, see Accessing Persistence in Web Applications .

4. Assemble the Application

To complete the overall JPA component development, you have to define your DataSource and DataSource alias, configure the persistence unit, and add your project to an Enterprise Archive by means of an Enterprise Application project (also called EAR project).

  1. Create an EAR project in the Developer Studio. You can also use an existing EAR project.

    For more information, see Creating Enterprise Application Projects .

  2. Define the DataSource and the DataSource alias for the persistence unit.

    For more information, see Working with DataSources .

  3. Configure the persistence unit in the persistence.xml .

    For more information, see Configuring the Persistence Unit in persistence.xml .

  4. Assemble the application in an Enterprise Archive.

    In the last stages of the development process, you put the application components together in Enterprise Archives. In the SAP NetWeaver Developer Studio, each component is represented by its corresponding project. The assembling of the application comprises adding the separate projects in the EAR project. Accordingly, you have to add your JPA (or EJB, or Dynamic Web) project to an EAR project as well.

    For more information, see Adding Modules to the Enterprise Application .

    For more information about assembling the application, see Assembling Components into Enterprise Applications .