Show TOC

Configuring the Persistence Unit in persistence.xmlLocate this document in the navigation structure

Prerequisites

You have:

  • Created the entities

  • Configured the DataSource and data source alias of your application

    If you are using the system DataSource, you must have defined a data source alias only.

Context

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. This is, for example, jta-data-source , which is the name of the DataSource that is used to connect to the database.

Note

If you do not specify the jta-data-source tag explicitly, a vendor-specific default is used. In AS Java, the preconfigured default name is SAP/JPA_DEFAULT , which is a DataSource alias that maps to the System DataSource.

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

Procedure

  1. In the Project Explorer , select the persistence.xml file.
  2. In the context menu, choose Open .
  3. To view the source code of the persistence.xml , choose the Source tab.
  4. Specify the data source for the persistence unit.

    Typically, you use the relevant data source alias name to specify the DataSource. Make sure that the value of the jta-data-source tag is the same as the value of the alias tag in the data-source-aliases.xml that you used to define the data source alias.

    Sample Code
                         <persistence-unit name="JPAModel">
            <jta-data-source>MY_DATA_SOURCE_ALIAS</jta-data-source>
    </persistence-unit>
                      
  5. If you are using versioning, configure the versioning properties.
    1. Specify the name of your version generator table as a value of the com.sap.jpa.versioning.generator.tablename property.

    2. (Optional) Specify the allocation size of the version generator, using the com.sap.jpa.versioning.generator.allocation-size property.

    For more information, see Versioning and Dirty Reads .

  6. (Optional) To list the entity classes in the persistence.xml , click the file with the secondary mouse button and choose Start of the navigation path JPA Tools Next navigation step Synchronize Class List End of the navigation path.
  7. If you want to configure lazy loading of single-valued relationships, see Lazy-Loading Entities with Single-Valued Relationships .
  8. (Optional) Configure additional persistence settings according to the JPA specification.
  9. Save your changes.

Example

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

Sample Code
               <?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>
        <properties>
                <property name="com.sap.jpa.versioning.generator.tablename" value="TMP_VERSION_GEN">
                <property name="com.sap.jpa.versioning.generator.allocation-size" value="1000">
        </properties>
        <class>com.sap.demo.package</class>
</persistence-unit>
</persistence>
            

Next Steps

persistence.xml