Home

 

Setting up the persistence.xml file

The persistence.xml file is used to configure the OpenJPA implementation. Because we want to use the RAD75JPA project later in the WebSphere server, we do not want to change that file. We can create a similar file in the RAD75JPATest project, so that it overwrites the file in the RAD75JPA project.

Right-click the src folder in the RAD75JPATest project and select New Æ Folder. Enter META-INF as Folder name and click Finish.

Copy the persistence.xml file from the RAD75JPA project into the META-INF folder of the RAD75JPATest project.

Open the persistence.xml file. In the editor (Example | 2-9).

Add transaction-type to the <persistence-unit> tag.
Remove the <jta-data-source> tag.
Add a <provider> tag.
Add four properties to setup connection to the ITSOBANK database and for logging.

Example 12-9 Persistence.xml file for OpenJPA

<?xml version="1.0" encoding="UTF-8"?>
<persistence ......>
	<persistence-unit name="RAD75JPA" transaction-type="RESOURCE_LOCAL">
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl
		</provider>
		<class>
		itso.bank.entities.Account</class>
		<class>
		itso.bank.entities.Customer</class>
		<class>
		itso.bank.entities.Transaction</class>
		<properties>
			<property name="openjpa.ConnectionURL"
					value="jdbc:derby:C:\7672code\database\derby\ITSOBANK" />
			<property name="openjpa.ConnectionDriverName"
					value="org.apache.derby.jdbc.EmbeddedDriver" />
			<property name="openjpa.ConnectionUserName" value="itso" />
			<property name="openjpa.Log" value="none" />
		</properties>
	</persistence-unit>
</persistence>

You can copy and paste the code from 7672code\jpa\test\persistence.xml.

Note: To use DB2 for the JPA entities, add these JAR files to the Libraries page:

<SQLLIB-HOME>/java/db2jcc.jar
<SQLLIB-HOME>/java/db2jcc_license_cu.jarr

Change the JPA properties in the persistence.xml file to:

<property name="openjpa.ConnectionURL"
							value="jdbc:db2://localhost:50000/ITSOBANK" />
<property name="openjpa.ConnectionDriverName" 
							value="com.ibm.db2.jcc.DB2Driver" />
<property name="openjpa.ConnectionUserName" value="db2admin" />
<property name="openjpa.ConnectionPassword" value="<password>"/>
<property name="openjpa.Log" value="none" />

ibm.com/redbooks