Show TOC

Procedure documentationConfiguring the Persistence Unit in the persistence.xml Locate this document in the navigation structure

 

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

End of the recommendation.

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

End of the note.

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

Procedure

  1. In the Project 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 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.

    Example Example

    <persistence-unit name="JPAModel">

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

    </persistence-unit>

    End of the example.
  4. 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.

    More information: Versioning and Dirty Reads

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

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

  7. Choose Save.

More Information

persistence.xml

Example

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

Example 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>

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

End of the example.