| <html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Middle Tier Highlights</title><meta content="DocBook XSL Stylesheets V1.76.0" name="generator"><link rel="home" href="index.html" title="A Guide to the GreenPages Sample"><link rel="up" href="ch03.html" title="Chapter 3. GreenPages Highlights"><link rel="prev" href="ch03.html" title="Chapter 3. GreenPages Highlights"><link rel="next" href="ch03s03.html" title="Testing Highlights"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table summary="Navigation header" width="100%"><tr><td align="left" width="20%"><a accesskey="p" href="ch03.html">Prev</a> </td><th align="center" width="60%"> </th><td align="right" width="20%"> <a accesskey="n" href="ch03s03.html">Next</a></td></tr></table><hr></div><div class="section" title="Middle Tier Highlights"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="highlights.middletier"></a>Middle Tier Highlights</h2></div></div></div><p> |
| In the middle tier of GreenPages, the DataSource bundle <code class="literal">greenpages.db</code> constructs a DataSource and |
| publishes it in the service registry and the JPA bundle <code class="literal">greenpages.jpa</code> uses the datasource to define a JPA entity manager |
| which provides an object-relational mapping between directory listings and the database. |
| The JPA bundle also uses declarative transaction management to ensure its persistence operations are performed inside transactions. |
| </p><div class="section" title="DataSource"><div class="titlepage"><div><div><h3 class="title"><a name="N104EE"></a>DataSource</h3></div></div></div><p> |
| The file <code class="filename">src/main/resources/META-INF/spring/module-context.xml</code> in the <code class="literal">greenpages.db</code> project |
| declares the Spring <span class="emphasis"><em>p-namespace</em></span>: |
| </p><pre class="programlisting"><beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.xsd" |
| xmlns:p="http://www.springframework.org/schema/p"> |
| </pre><p> |
| which is then used to define properties of a datasource bean: |
| </p><pre class="programlisting"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" |
| p:driverClassName="org.h2.Driver" p:url="jdbc:h2:~/greenpages-db/greenpages" |
| p:username="greenpages" p:password="pass" |
| init-method="createDataSource" destroy-method="close"/> |
| </pre><p> |
| </p><p> |
| The file <code class="filename">src/main/resources/META-INF/spring/osgi-context.xml</code> publishes the datasource bean as a service in the |
| service registry using Spring DM: |
| </p><pre class="programlisting"><osgi:service ref="dataSource" interface="javax.sql.DataSource"/> |
| </pre><p> |
| </p></div><div class="section" title="EntityManager"><div class="titlepage"><div><div><h3 class="title"><a name="N10513"></a>EntityManager</h3></div></div></div><p> |
| The <code class="classname">greenpages.jpa.JpaDirectory</code> class in the folder <code class="filename">src/main/java</code> of the |
| <code class="literal">greenpages.jpa</code> project uses the <code class="literal">@Repository</code> annotation to make it eligible for Spring DataAccessException translation |
| (which abstracts implementation-specific persistence exceptions to protect the application from details of the persistence implementation): |
| </p><pre class="programlisting">@Repository |
| final class JpaDirectory implements Directory { |
| </pre><p> |
| and also declares an entity manager which will be injected by Spring: |
| </p><pre class="programlisting">@PersistenceContext |
| private EntityManager em; |
| </pre><p> |
| </p><p> |
| The file <code class="filename">src/main/resources/META-INF/spring/module-context.xml</code> in the <code class="literal">greenpages.jpa</code> project |
| declares an entity manager factory based on EclipseLink JPA: |
| </p><pre class="programlisting"><bean id="entityManagerFactory" |
| class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" |
| p:dataSource-ref="dataSource"> |
| <property name="jpaVendorAdapter"> |
| <bean id="jpaVendorAdapter" |
| class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" |
| p:databasePlatform="org.eclipse.persistence.platform.database.HSQLPlatform" |
| p:showSql="true"/> |
| </property> |
| </bean> |
| </pre><p> |
| </p><p> |
| The same file enables scanning for annotations, including <code class="literal">@PersistenceContext</code>: |
| </p><pre class="programlisting"><context:annotation-config/> |
| </pre><p> |
| enables load-time weaving, which is needed by the entity manager factory: |
| </p><pre class="programlisting"><context:load-time-weaver aspectj-weaving="on"/> |
| </pre><p> |
| and specifies a bean post processor to perform exception translation for <code class="literal">@Repository</code> classes: |
| </p><pre class="programlisting"><bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> |
| </pre><p> |
| </p><p> |
| The file <code class="filename">src/main/resources/META-INF/persistence.xml</code> defines a persistence unit for a <code class="classname">JpaListing</code> |
| directory listing class. |
| </p><pre class="programlisting"><persistence 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" |
| version="1.0"> |
| |
| <persistence-unit name="GreenPages" transaction-type="RESOURCE_LOCAL"> |
| <class>greenpages.jpa.JpaListing</class> |
| </persistence-unit> |
| |
| </persistence> |
| </pre><p> |
| </p><p> |
| The file <code class="filename">src/main/resources/META-INF/orm.xml</code> defines an entity mapping for the <code class="classname">JpaListing</code> class. |
| </p><pre class="programlisting"><entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm |
| http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" |
| version="1.0"> |
| <package>greenpages.jpa</package> |
| <entity class="greenpages.jpa.JpaListing" name="Listing"> |
| <table name="LISTING"/> |
| <attributes> |
| <id name="listingNumber"> |
| <column name="LISTING_NUMBER"/> |
| <generated-value strategy="TABLE"/> |
| </id> |
| <basic name="firstName"> |
| <column name="FIRST_NAME"/> |
| </basic> |
| ??? |
| </attributes> |
| </entity> |
| </entity-mappings> |
| </pre><p> |
| </p></div><div class="section" title="Transaction Management"><div class="titlepage"><div><div><h3 class="title"><a name="N10575"></a>Transaction Management</h3></div></div></div><p> |
| The <code class="classname">greenpages.jpa.JpaDirectory</code> class in the folder <code class="filename">src/main/java</code> of the |
| <code class="literal">greenpages.jpa</code> project uses the <code class="literal">@Transactional</code> annotation to provide transaction demarcation |
| (beginning and committing a transaction around each method in this case): |
| </p><pre class="programlisting">@Transactional |
| ??? |
| final class JpaDirectory implements Directory { |
| </pre><p> |
| </p><p> |
| The file <code class="filename">src/main/resources/META-INF/spring/module-context.xml</code> enables AspectJ weaving for transaction demarcation: |
| </p><pre class="programlisting"><tx:annotation-driven mode="aspectj"/> |
| </pre><p> |
| and specifies that the Spring <code class="classname">JpaTransactionManager</code> should be used and associated with the entity manager factory: |
| </p><pre class="programlisting"><bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" |
| p:entityManagerFactory-ref="entityManagerFactory"/> |
| </pre><p> |
| </p></div></div><div class="navfooter"><hr><table summary="Navigation footer" width="100%"><tr><td align="left" width="40%"><a accesskey="p" href="ch03.html">Prev</a> </td><td align="center" width="20%"><a accesskey="u" href="ch03.html">Up</a></td><td align="right" width="40%"> <a accesskey="n" href="ch03s03.html">Next</a></td></tr><tr><td valign="top" align="left" width="40%"> </td><td align="center" width="20%"><a accesskey="h" href="index.html">Home</a></td><td valign="top" align="right" width="40%"> </td></tr></table></div></body></html> |