Home

 

Setting up the persistence.xml file

We have to modify the persistence.xml file because the JUnit test runs in Java SE, not in the server. Instead of connecting to the database through a data source, we connect directly through a JDBC driver. We could modify the persistence.xml file in the RAD75JPA project, but it is better to leave that file configured for the data source in the server, and place a new file into the RAD75JUnit project, overwriting the file in the RAD75JPA project.

In the Package Explorer, right-click the RAD75JUnit Æ src folder and select New Æ Folder. Type META-INF as folder name and click Finish.

Copy the file RAD75JPA/src/META-INF/persistence.xml to RAD75JUnit/src/META-INF.

Open the persistence.xml file (in RAD75JUnit/src/META-INF), and change it as shown in Example | 3-7. The updated file is available in c:\7672code\junit\jpa\persistence.xml.

Example 23-7 JPA persistence.xml configured for Derby using OpenJPA

<?xml version="1.0" encoding="UTF-8"?>
<persistence .....>
	<persistence-unit name="RAD75JPA" transaction-type="RESOURCE_LOCAL">
		
<jta-data-source>jdbc/itsobank</jta-data-source>
		<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>
		<class>itso.bank.entities.Debit</class>
		<class>itso.bank.entities.Credit</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.Log" value="none" />
		</properties>
	</persistence-unit>
</persistence>

To use the ITSOBANK database in DB2, the properties would be:

openjpa.ConnectionURL: jdbc:db2://localhost:50000/ITSOBANK

openjpa.ConnectionDriverName: com.ibm.db2.jcc.DB2Driver

openjpa.ConnectionUserName: db2admin | #160; | #160; | #160; | #160; | #160; | #160; | #160; | or similar)

openjpa.ConnectionPassword: <xxxxxxxx>

To see the SQL statements that are issued, set openjpa.Log to the value SQL=TRACE.
ibm.com/redbooks