Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This is an example of a JSP-page calling an Enity Bean that uses annotations. The result looks like this:

Application Contents

First, let us take a look at the PhoneBook Entity Bean that represents a table in the database. Each instance of PhoneBook is a record of the table.
PhoneBook.java uses the

...

persistence.xml will specify the name of the PersistenceUnit. This name is used when referencing for the EntityManagerFactory. I have denoted it as PhonePU. For some reason I could not get it to reference with jta-data-source. So the alternative method is to explicitly specify the ConnectionURL, ConnectionDriverName, and ConnectionUserName. I added an extra property called SynchronizeMappings so that the data in the database will not be overwritten.

SEE BELOW FOR POSSIBLE SOLUTION

Code Block
xml
xml
borderStylesolid
titlepersistence.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"
		xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="PhonePU">
		<description>Phone Book</description>
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<class>org.apache.geronimo.samples.myphonebookpak.PhoneBook</class>
		<properties>
			<property name="openjpa.ConnectionURL" value="jdbc:derby:PhoneBookDB" />
			<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" />
			<property name="ConnectionUserName" value="app" />
			<property name="openjpa.jdbc.SynchronizeMappings" value="false" />
		</properties>
	</persistence-unit>
	<!--
	<jta-data-source>PhoneBookPool</jta-data-source>
	<non-jta-data-source>PhoneBookPool</non-jta-data-source>
	-->
</persistence>

...

To test the sample web application open a browser and type http://localhost:8080/myphonebook.

Untested Instructions for using jta-datasource in persistence.xml

This app does not use openjpa sequences so apparently you can get by with only a jta-datasource. In my experience apps that do use openjpa sequences to supply primary key values also need a non-jta-datasource. When deploying such a non-jta-datasource check the plan and make sure it says <no-transaction/> rather than <local-transaction/> or <xa-transaction/>.

For this app, use the console to deploy a datasource using the database of your choice. You need to supply a name such as "MyDS" for your datasource. At the end you should have a name for the module your datasource is in such as console.dbpool/MyDS/1.0/rar. You need to do two things so geronimo can hook up to your datasource:

1. Include the module name in the app dependencies so geronimo knows to look in the new datasource module for the datasource. So, the openejb-jar.xml should look something like:

Code Block
xml
xml
borderStylesolid
titleopenejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar
		xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1"
		xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
		xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0"
		xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
		xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
	<sys:environment>
		<sys:moduleId>
			<sys:groupId>org.apache.geronimo.samples</sys:groupId>
			<sys:artifactId>MyPhonebookBean</sys:artifactId>
			<sys:version>1.0</sys:version>
			<sys:type>car</sys:type>
		</sys:moduleId>
                <sys:dependencies>
                    <sys:dependency>
			<sys:groupId>console.dbpool</sys:groupId>
			<sys:artifactId>MyDS</sys:artifactId>
			<sys:version>1.0</sys:version>
			<sys:type>rar</sys:type>
                    </sys:dependency>
	</sys:environment>
</openejb-jar>

2. specify the name of the datasource in persistence.xml, something like

Code Block
xml
xml
borderStylesolid
titlepersistence.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"
		xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="PhonePU">
		<description>Phone Book</description>
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
                <jta-datasource>MyDS</jta-datasource>
		<class>org.apache.geronimo.samples.myphonebookpak.PhoneBook</class>
		<properties>
			<property name="openjpa.jdbc.SynchronizeMappings" value="false" />
		</properties>
	</persistence-unit>
	<!--
	<jta-data-source>PhoneBookPool</jta-data-source>
	<non-jta-data-source>NoTXPhoneBookPool</non-jta-data-source>
	-->
</persistence>