Show TOC Start of Content Area

Procedure documentation Configuring the Persistence Unit in the persistence.xml  Locate the document in its SAP Library structure

Use

The data model of a JPA application typically consists of several related entity classes, which have to be mapped together to one and the same database. These entity classes form a logical unit, which is called a persistence unit. Within a JPA application, you have to define the persistence unit by the persistence.xml configuration file. It is possible to list the entities explicitly that form the persistence unit. If you do not do so, the JPA implementation scans the application for entities and detects them automatically.

The name of the persistence unit specified in the persistence-unit tag can be chosen arbitrarily.

Recommendation

Due to the specifics of the Dali JPA Tools Project, we recommend that you define only one persistence unit within the persistence.xml file.

The persistence.xml is also the configuration file where you can use tags to define any global settings for the persistence unit. These are, for example, jta-data-source, which is the name of the DataSource that is used to connect to the database.

Caution

If the name of the JTA data source is not specified explicitly, a vendor-specific default is used. In this case, no DataSource with this name is preconfigured in SAP AS Java. Therefore, in general, an application should specify the data source name explicitly. With SAP JPA 1.0 this default name is SAP/JPA_DEFAULT.

You can also add other configuration elements to the persistence.xml file, such as transaction type, provider, non-jta-data-source, mapping-file, jar-file, class, exclude-unlisted-classes, and so on. The only mandatory element is the name property of the persistence-unit tag, which the SAP NetWeaver Developer Studio includes in the file automatically.

For a complete listing of elements and their semantics, refer to the JPA specification on Sun Microsystems’ web site:

Link to external website

http://java.sun.com

In the Developer Studio, the persistence.xml file is located under the META-INF directory of the JPA project or another project with JPA support enabled.

Prerequisites

You have:

      Created your entities

      Defined your DataSource and DataSource alias

Procedure

...

       1.      In the Package Explorer, select the persistence.xml file.

       2.      In the context menu, choose Open.

       3.      Specify the Data Source for the persistence unit.

Typically, you use the relevant DataSource alias name to specify the DataSource.

Example

<persistence-unit name="JPAModel">

  <jta-data-source>MY_DATA_SOURCE_ALIAS</jta-data-source>

</persistence-unit>

       4.      (Optional) To list the entity classes in the persistence.xml, click the file with the secondary mouse button and choose JPA Tools Synchronize Classes.

       5.      (Optional) Specify additional configuration elements according to the JPA specification.

       6.      Choose Save.

Example

The following is an example of how the basic persistence.xml code and the persistence unit definition look like:

Example

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

  <persistence-unit name="JPAModel">

     <jta-data-source>MY_DATA_SOURCE</jta-data-source>

<class>com.sap.demo.package</class>

  </persistence-unit>

</persistence>

End of Content Area