Define a persistent unit

A persistent unit is a collection of entities that are managed together. It specifies the persistent provider name, entity class names, and properties like the database connection URL, driver, user and password etc. It is defined in a XML descriptor file and the file name is called persistence.xml. This XML file should be placed in the META-INF folder.

In the Logistics Demo the persistence.xml file is located in the order-ws-su service unit and the order-processor-su service unit.

Example 1.3 Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="1.0">

 <persistence-unit name="order-processor" transaction-type="RESOURCE_LOCAL">
 <provider>
   org.apache.openjpa.persistence.PersistenceProviderImpl
 </provider>

 <class>com.iona.fuse.demo.logisticx.model.Order</class>
 <class>com.iona.fuse.demo.logisticx.model.LineItem</class>
 <class>com.iona.fuse.demo.logisticx.model.OrderStatus</class>

 <properties>
   <property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/database;create=true">
   <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver"/>
   <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
   <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO, SQL=TRACE"/>
 </properties>
</persistence-unit>

</persistence>
&nbsp;
  • The root element is called persistence which contains one or more persistent units. Each unit can be used by different JPA vendors or can be used to persist to different databases. Each persistent unit defines the configuration for the entity managers created by the persistent unit's entity manager factory.
  • The <persistent-unit-name> specifies the name of the unit. The transaction-type attribute specifies whether to use managed (JTA) or local (RESOURCE_LOCAL) transaction management. Here we are going to manage the transactions ourselves so we use local transaction management.
  • The specific persistent provider name is specified in the <provider> tag.  Set it to org.apache.openjpa.persistence.PersistenceProviderImpl for OpenJPA.
  • <class> tags specifty the entity classes.
  • The <properties> tag specifies the vendors specific connection properties.
    • openjpa.ConnectionURL - The JDBC URL for the database
    • openjpa.ConnectionDriverName - The full class name of either the JDBC java.sql.Driver, or a javax.sql.DataSource implementation to use to connect to the database.
    • openjpa.jdbc.SynchronizeMappings - Controls whether OpenJPA will attempt to run the mapping tool on all persistent classes to synchronize their mappings and schema at runtime.
    • openjpa.Log - A plugin string describing a org.apache.openjpa.lib.log.LogFactory to use for logging.
  • For more information on the persistence properties see the OpenJPA documentation.

Here we are going to connect to an database that is run by the Derby Network Server on port 1527 on the localhost. We use the ClientDriver to connect in this case.








 

Labels

 
(None)