Show TOC Start of Content Area

Procedure documentation Developing Persistence Using JPA  Locate the document in its SAP Library 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 and so on), 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 in order to distinguish it from other regular Java objects that you might use within your application. You use Java annotations as a way of defining how the entity maps to your existing database table(s).

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

The SAP NetWeaver Developer Studio includes the open-source Dali project, version 0.5, which provides object/relational persistence tools for creating and modifying entities according to the JPA 1.0 specification.

Recommendation

       From the design time perspective, we recommend that you use the Dali object/relational tools for quick prototyping or generation of basic skeleton of entities only.

More information about limitations:

Generating Entities from Database Tables

Mapping Entities to Database Tables

       From the runtime perspective, we recommend that you observe the restrictions imposed by the JPA implementation underlying the SAP NetWeaver Application Server when developing persistent entities.

More information: JPA Implementation Considerations

Procedure

1. Prepare for the Development of Entities

...

       1.      In the Developer Studio, define the connection to the database to enable the mapping of entities to the database schema.

More information: Creating Database Connections

       2.      Create the project to wrap the entities.

You can:

       Use an existing EJB 3.0 project.

       Create a new EJB 3.0 project.

More information: Creating EJB 3.0 Projects

       3.      Add persistence to the project.

This makes the project aware that it includes persistence units and maps it to the database schema you want to work with.

More information: Adding Persistence to Projects

2. Create Persistent Entities

Entities consist of a set of properties and their corresponding getter and setter methods. Using annotations, you can define the object/relational mapping between properties and columns of the database tables.

There are two approaches for the initial creation of entities with basic object/relational mapping. You can:

      Automatically generate entities from existing database tables.

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

More information: Generating Entities from Database Tables

      Create entities as POJOs using Java annotations.

Using this approach is suitable for developers who are comfortable with object-oriented programming rather than database programming.

More information: Creating Persistent Entities

3. Define the Object/Relational Mapping

The persistence tier 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 tier is also known as object/relational mapping (because it maps Java objects to relational data).

You can configure the object/relational mapping using the corresponding annotations. You can:

      Fine-tune the persistence properties.

More information: Modifying Persistence Properties

      Create relationships between entities.

More information: Mapping Relationships

      Specify inheritance between entity classes.

More information: Mapping Inheritance

      Enable JPA versioning on data sources.

More information: Versioning with read_uncommitted Isolation Level

4. Work with Entities Using the Entity Manager

In the other tiers (business logic and user interfaces) of the same Java EE 5 application or external applications, you can access the persistence functionality using the entity manager. The entity manager allows you to synchronize the persistent state of the entities to the database. It manages entity objects in the following way:

      When you need to retrieve information from the database, it returns the respective entities.

      When you need to update data in the database, you pass the updated entity objects to the entity manager, so it performs the necessary SQL queries for updating the information. In the business tier, you can also perform explicit queries using an SQL-like query language.

You can perform the following subtasks:

...

       1.      Get an entity manager.

More information: Obtaining an Entity Manager Instance

       2.      Manage entity instances.

More information: Managing Entity Instances

       3.      Optionally, create and execute queries using the query language.

More information: Creating and Executing Queries

End of Content Area