<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en">
<title>Max Poon&apos;s Blog</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/maxpoon/" />
<modified>2007-08-19T15:56:03Z</modified>
<tagline></tagline>
<id>tag:weblogs.java.net,2007:/blog/maxpoon/406</id>
<generator url="http://www.movabletype.org/" version="3.01D">Movable Type</generator>
<copyright>Copyright (c) 2007, maxpoon</copyright>
<entry>
<title>Extending the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 4 - Switching from Hibernate JPA to Glassfish JPA/TopLink Essentials (with Optional NetBeans Project Resources and Source Package Path Names Renaming)</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/maxpoon/archive/2007/08/extending_the_n_4.html" />
<modified>2007-08-19T15:56:03Z</modified>
<issued>2007-08-11T17:29:57Z</issued>
<id>tag:weblogs.java.net,2007:/blog/maxpoon/406.8011</id>
<created>2007-08-11T17:29:57Z</created>
<summary type="text/plain">SimpleJpaHibernateApp is developed based on JSR 220 Java Persistence API with optional Hibernate-specific options, e.g. in persistence.xml or @Cache Annotations for caching applied.  Hence, due to the pluggability of the Java Persistence API architecture, JPA-compliant applications such as SimpleJpaHibernateApp can easily be switched to make use of other Java Persistence providers such as Glassfish JPA.  This article describes how this can be done.</summary>
<author>
<name>maxpoon</name>

<email>maxpoon@sun.com</email>
</author>
<dc:subject>Community: Java Enterprise</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/maxpoon/">
<![CDATA[<h2>Background</h2>
It was shown in the previous article<br>
<ul>
  <li><a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_3.html">Extending
the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 3 - Enabling
JMX Monitoring on Hibernate v3 and Ehcache 1.3.0 on
"SimpleJpaHibernateApp"</a></li>
</ul>
how to configure Java Persistence API (JPA) based application to use
Hibernate and Ehcache as the
JPA and caching implementation provider respectively. <br>
<br>
SimpleJpaHibernateApp is developed based on JSR 220 Java Persistence
API with optional Hibernate-specific options, e.g. in <span
 style="font-family: monospace;">persistence.xml</span> or <span
 style="font-family: monospace;">@Cache</span> Annotations
for caching applied.&nbsp; Hence, due to the pluggability of the Java
Persistence API architecture, JPA-compliant applications such as
SimpleJpaHibernateApp can easily be switched to make use of other Java
Persistence providers such as Glassfish JPA.&nbsp; This article
describes how this can be done.<br>
<br>
<h2>Glassfish JPA / TopLink Essentials</h2>
As mentioned in <a
 href="http://www.oracle.com/technology/products/ias/toplink/jpa/index.html">Oracle's
TopLink JPA web page</a>, <span style="font-style: italic;">"TopLink
Essentials is the open-source community edition of Oracle's TopLink
product. It provides the JPA functionality for the EJB 3.0 Reference
Implementation."</span>.<br>
<br>
As also mentioned in <a
 href="https://glassfish.dev.java.net/javaee5/persistence/">Glassfish
Java Persistence API Implementation web page</a>, TopLink Essentials is
<span style="font-style: italic;">"the Java Persistence API
implementation at the GlassFish community"</span>, and adopted as the
JPA Reference Implementation within Glassfish Java EE Reference
Implementation.<br>
<br>
<h2>Configuring SimpleJpaHibernateApp to use Glassfish JPA<br>
</h2>
The
configuration works involve the following suggested steps :<br>
<ul>
  <li><a href="#Step_1_Optional_-_Renaming_to_SimpleJ">Step 1 -
(Optional) Renaming "SimpleJpaHibernateApp" project name and Java
package path names to "SimpleJpaToplinkApp"</a> <br>
  </li>
  <li><a href="#Step_2_-_Changing_Java_Persistence">Step 2 - Changing
Java Persistence Provider in Persistence Unit</a> <br>
  </li>
  <li><a href="#Step_3_-_Optional_Changing_DB_Schema">Step 3 -
(Optional) Changing DB Schema Name to "APP_SimpleJpaToplinkApp", and
Fixing the Web App Context Path and Referencing URIs</a></li>
  <li><a href="#Step_4_-_Compile_and_Deploy">Step 4 - Compiling and
Deploying the new "SimpleJpaToplinkApp"</a><br>
  </li>
</ul>
[Note: Only Step 2 above is the minimally necessary step generally,
while Steps 1
&amp; 3 are to fix the NetBeans project and Java package path naming
consistencies (applicable to applications like "SimpleJpaHibernateApp"
where the NetBeans project name, Java package path names and web app
context URI are preferred to be changed as well.]<br>
<br>
<h3><a name="Step_1_Optional_-_Renaming_to_SimpleJ"></a><span
 style="font-weight: bold;">Step 1 - (Optional) Renaming
"SimpleJpaHibernateApp" project name and Java package path names
to "SimpleJpaToplinkApp" </span></h3>
To make the NetBeans project and package naming consistent, the
"SimpleJpaHibernateApp" project name and Java package path
names are first suggested to be changed to, e.g.
"SimpleJpaToplinkApp".&nbsp; One way to do this is as follows.<br>
<br>
(1A) First, make a complete copy of all the files and sub-directories
of the original NetBeans project "SimpleJpaHibernateApp" so that the
copy resides under a directory, say
"<span style="font-family: monospace;">...../SimpleJpaToplinkApp</span>"
instead of the original "<span style="font-family: monospace;">...../SimpleJpaHibernateApp</span>".<br>
<br>
(1B) Open the copy of "SimpleJpaHibernateApp" (now under the directory "<span
 style="font-family: monospace;">...../SimpleJpaToplinkApp</span>"),
right-click on NetBeans project "SimpleJpaHibernateApp" and select
"Rename" to rename it to "SimpleJpaToplinkApp".<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 1B - Renaming
"SimpleJpaHibernateApp" to "SimpleJpaToplinkApp" in NetBeans IDE</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaToplinkApp_20070811/1_SimpleJpaToplinkApp_renameProj_650.png"
 title="" alt="1_SimpleJpaToplinkApp_renameProj_650.png"
 style="width: 650px; height: 447px;"><br>
<br>
This changes the NetBeans project name in <span
 style="font-family: monospace;">...../SimpleJpaToplinkApp/nbproject/project.xml</span>
to <span style="font-family: monospace;">SimpleJpaToplinkApp</span> as
shown below.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Code Listing 1B - New project
name in nbproject/project.xml (changes in <span
 style="color: rgb(255, 0, 0);">red</span>)</span><br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version="1.0"
encoding="UTF-8"?&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;project
xmlns="http://www.netbeans.org/ns/project/1"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;
&lt;type&gt;org.netbeans.modules.web.project&lt;/type&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &lt;configuration&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;data
xmlns="http://www.netbeans.org/ns/web-project/3"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;name&gt;<span style="font-weight: bold; color: rgb(255, 0, 0);">SimpleJpaToplinkApp</span>&lt;/name&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
....</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;/data&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;
&lt;/configuration&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;/project&gt;</span><br>
      </td>
    </tr>
  </tbody>
</table>
<br>
(1C) Change the Persistence Unit name from
"SimpleJpaHibernateAppPU" to "SimpleJpaToplinkAppPU" in <span
 style="font-family: monospace;">persistence.xml</span><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr style="font-weight: bold;">
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">Code
Listing 1C - Changing Persistence-Unit name from
"SimpleJpaHibernateAppPU"
to "SimpleJpaToplinkAppPU" in persistence.xml</td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version="1.0"
encoding="UTF-8"?&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;persistence
version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" <br>
&nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br>
&nbsp;xsi:schemaLocation="http://java.sun.com/xml/ns/persistence<br>
&nbsp;http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &lt;persistence-unit
name="<span style="font-weight: bold; color: rgb(255, 0, 0);">SimpleJpaToplinkAppPU</span>"
transaction-type="JTA"&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;provider&gt;<span
 style="color: rgb(0, 0, 0); font-family: monospace;">org.hibernate.ejb.HibernatePersistence</span><span
 style="font-family: monospace;">&lt;/provider&gt;</span></span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;jta-data-source&gt;jdbc/sample&lt;/jta-data-source&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
....<br>
&nbsp;
&lt;/persistence-unit&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;/persistence&gt;</span></td>
    </tr>
  </tbody>
</table>
<br>
[Note: Steps (2A) and (2B) below involve also changes in <span
 style="font-family: monospace;">persistence.xml</span> and hence may
be done here as well, but are left to be done later as they are
necessary steps to be grouped in Step 2.]<br>
<br>
(1D) Re-open the "renamed SimpleJpaToplinkApp" project in NetBeans IDE,
then change the Java package path name "SimpleJpaHibernateApp" to
"SimpleJpaToplinkApp" by right-clicking on the concerned Source
Packages and selecting [Refactor] &gt; [Rename...] to change the
package names from :<br>
<ul style="font-family: monospace;">
  <li>SimpleJpaHibernateApp.agents to SimpleJpaToplinkApp.agents</li>
  <li>SimpleJpaHibernateApp.controller to SimpleJpaToplinkApp.controller</li>
  <li>SimpleJpaHibernateApp.entities to SimpleJpaToplinkApp.entities</li>
</ul>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 1D - Using NetBeans IDE "Refactor"
to Rename Java Source Packages path names</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaToplinkApp_20070811/1_SimpleJpaToplinkApp_refactorPackageName_650.png"
 title="" alt="1_SimpleJpaToplinkApp_refactorPackageName_650.png"
 style="width: 650px; height: 450px;"><br>
<br>
<h3><a name="Step_2_-_Changing_Java_Persistence"></a>Step 2 - Changing
Java Persistence Provider in Persistence-Unit</h3>
(2A) Double click on [persistence.xml] under [Configuration Files] in
[Projects] Window to open up the [Persistence Units] Design Panel, then
change the Persistence Provider for SimpleJpaToplinkAppPU from
"Hibernate" to "TopLink(default)" as follows.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 2A - </span><span
 style="font-weight: bold;"> Using Netbeans IDE Design Panel for </span><span
 style="font-weight: bold;">persistence.xml </span><span
 style="font-weight: bold;"> to Change</span><span
 style="font-weight: bold;"> Java
Persistence Provider</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaToplinkApp_20070811/2_SimpleJpaToplinkApp_switchTopLink1_650.png"
 title="" alt="2_SimpleJpaToplinkApp_switchTopLink1_650.png"
 style="width: 650px; height: 486px;"><br>
<br>
(2B) Switch from "Design" to "XML" Panel for <span
 style="font-family: monospace;">persistence.xml</span>, observe
the change of <span style="font-family: monospace;">&lt;provider&gt;</span>
element, replace the Hibernate caching
properties with TopLink caching, and select [File] &gt; [Save] to save
the modified <span style="font-family: monospace;">persistence.xml</span>
as shown :<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Code Listing 2B - Changing &lt;provider&gt;
and &lt;properties&gt; for caching in persistence.xml</span><br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version="1.0"
encoding="UTF-8"?&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;"></span><span
 style="font-family: monospace;">&lt;persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" <br>
&nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br>
&nbsp;xsi:schemaLocation="http://java.sun.com/xml/ns/persistence<br>
&nbsp;http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &lt;persistence-unit
name="<span style="font-weight: bold; color: rgb(255, 0, 0);">SimpleJpaToplinkAppPU</span>"
transaction-type="JTA"&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;"></span><span
 style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;provider&gt;<span
 style="font-weight: bold; color: rgb(255, 0, 0);">oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</span><br>
&nbsp;&nbsp;&nbsp; &lt;/provider&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;jta-data-source&gt;jdbc/sample&lt;/jta-data-source&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;class&gt;simpleJpaToplinkApp.entities.Manufacturer&lt;/class&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;class&gt;simpleJpaToplinkApp.entities.ProductCode&lt;/class&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;class&gt;simpleJpaToplinkApp.entities.Product&lt;/class&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;properties&gt;<br>
      <span style="color: rgb(255, 0, 0); font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;property name="toplink.cache.type.Manufacturer"
value="HardWeak"/&gt;</span><br
 style="color: rgb(255, 0, 0); font-weight: bold;">
      <span style="color: rgb(255, 0, 0); font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;property name="toplink.cache.type.ProductCode" value="Full"/&gt;</span><br
 style="color: rgb(255, 0, 0); font-weight: bold;">
      <span style="color: rgb(255, 0, 0); font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;property name="toplink.cache.type.Product" value="SoftWeak"/&gt;</span><br
 style="color: rgb(255, 0, 0); font-weight: bold;">
&nbsp;&nbsp;&nbsp; &lt;/properties&gt;<br>
      </span><span
 style="font-family: monospace; text-decoration: line-through;"></span><span
 style="font-family: monospace;">&nbsp;
&lt;/persistence-unit&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;/persistence&gt;</span><br>
      </td>
    </tr>
  </tbody>
</table>
<br>
While this configuration (similar to corresponding ones for Hibernate)
in <span style="font-family: monospace;">persistence.xml</span> will
enable basic caching for specified JPA entity classes, configuration
for Glassfish JPA is a bit simplier (and also more "restrictive") as
Glassfish JPA has already included and can only uses its own caching
implementation instead of requiring/having the choice to
specify/configure for an external caching implementation such as
Ehcache.<br>
<br>
Any <span style="font-family: monospace;">@Cache</span> annotations
used in entity class to Hibernate caching should be removed as they are
not supported by Glassfish JPA / TopLink Essentials (though TopLink may
have its own <span style="font-family: monospace;">@Cache</span>
annotations).&nbsp; Use <span style="font-family: monospace;">&lt;properties&gt;</span>
configuration in <span style="font-family: monospace;">persistence.xml</span>
to enable caching in Glassfish JPA.<br>
<br>
Refer to <a href="#Further_Resources">Further Resources</a> for more
information on Glassfish JPA / TopLink Essentials caching architecture
and options such as "Full", "HardWeak", and "SoftWeak".<br>
<br>
(2C) Change SimpleJpaToplinkApp's referencing Library from Hibernate
libraries to TopLink
Essentials library.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 2C - SimpleJpaToplinkApp changed to
use TopLink Essentials library in NetBeans IDE</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaToplinkApp_20070811/2_SimpleJpaToplinkApp_switchTopLink3a_650.png"
 title="" alt="2_SimpleJpaToplinkApp_switchTopLink3a_650.png"
 style="width: 650px; height: 457px;"><br>
<br>
(2D) Since JMX monitoring is not currently used in SimpleJpaToplinkApp,
from all 3 controllers (ManufacturerController, ProductCodeController,
ProductController), comment out statements :<br>
<ul style="font-family: monospace;">
  <li>import simpleJpaToplinkApp.agents.JmxAgent;</li>
  <li>initJMX();</li>
  <li>void initJMX() { ... }<br>
  </li>
</ul>
and change <br>
<ul style="font-family: monospace;">
  <li>@PersistenceUnit(unitName = "SimpleJpaHibernateAppPU")</li>
</ul>
to <br>
<ul style="font-family: monospace;">
  <li>@PersistenceUnit(unitName = "SimpleJpaToplinkAppPU")</li>
</ul>
Then, right-click on [simpleJpaToplink.App.JmxAgent] and select
[Refactor] &gt; [Safely Delete...] to delete JmxAgent from the NetBeans
project.<br>
<br>
<h3><a name="Step_3_-_Optional_Changing_DB_Schema"></a>Step 3 -
(Optional) Changing DB Schema Name to APP_SimpleJpaToplinkApp, and
Fixing the Web App Context Path and Referencing URIs<br>
</h3>
(3A) For naming consistency, change the DB schema name to from
"APP_SimpleHibernateApp" to "APP_SimpleJpaToplinkApp" using NetBeans
IDE "Rename" function.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 3A1 - Using NetBeans IDE "Rename" to
Change DB Schema Name</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaToplinkApp_20070811/3_SimpleJpaToplinkApp_changeSchemaName1_650.png"
 title="" alt="3_SimpleJpaToplinkApp_changeSchemaName1_650.png"
 style="width: 650px; height: 451px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 3A2 - Using NetBeans IDE "Rename" to
Change DB Schema Name</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaToplinkApp_20070811/3_SimpleJpaToplinkApp_changeSchemaName2_650.png"
 title="" alt="2_SimpleJpaToplinkApp_switchTopLink5_650.png"
 style="width: 650px; height: 457px;"><br>
<br>
(3B) Also, the web application context path can optionally be fixed to
"SimpleToplinkApp" (as below), and subsequently all JSF views
referencing previous context URI and text labels "SimpleHibernateApp"
are required to be changed to "SimpleToplinkApp".<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 3B - Changing Context Path</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaToplinkApp_20070811/3_SimpleJpaToplinkApp_switchTopLink5_650.png"
 title="" alt="3_SimpleJpaToplinkApp_switchTopLink5_650.png"
 style="width: 650px; height: 457px;"><br>
<br>
<h3><a name="Step_4_-_Compile_and_Deploy"></a>Step 4 - Compiling and
Deploying the new "SimpleJpaToplinkApp"<br>
</h3>
Finally, the "new" SimpleJpaToplinkApp project should look as shown in
Figure 3B, and
is ready to be compiled and deployed.&nbsp; The service will be
available at deployed Glassfish HTTP service port with context URI "<span
 style="font-family: monospace;">/SimpleJpaToplinkApp</span>" as
specified above.<br>
<br>
<h2><a name="Further_Resources"></a>Further Resources</h2>
<ul>
  <li><a
 href="http://www.oracle.com/technology/products/ias/toplink/jpa/index.html">Oracle:
TopLink JPA</a></li>
  <li><a href="https://glassfish.dev.java.net/javaee5/persistence/">Glassfish
Java Persistence API Implementation web page</a></li>
  <li><span class="topstoryhead"><a
 href="http://www.oracle.com/technology/tech/java/newsletter/articles/toplink/toplink_caching_locking.html">Oracle:
Overview of TopLink Caching and Locking</a> </span></li>
  <li><span class="topstoryhead"><a
 href="http://www.oracle.com/technology/products/ias/toplink/doc/1013/main/_html/cachun.htm#CHEJAEBH">Oracle
TopLink Developer's Guide - Understanding the Cache</a></span></li>
  <li><span class="topstoryhead"><a
 href="http://weblogs.java.net/blog/guruwons/archive/2006/09/understanding_t.html">Wonseok
Kim's Blog - Understanding the cache of TopLink Essentials(GlassFish
JPA)</a> <br>
    </span></li>
</ul>]]>

</content>
</entry>
<entry>
<title>Extending the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 3 - Enabling JMX Monitoring on Hibernate v3 and Ehcache 1.3.0 on &quot;SimpleJpaHibernateApp&quot;</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_3.html" />
<modified>2007-08-20T19:07:18Z</modified>
<issued>2007-06-27T19:21:13Z</issued>
<id>tag:weblogs.java.net,2007:/blog/maxpoon/406.7750</id>
<created>2007-06-27T19:21:13Z</created>
<summary type="text/plain">This is the continuation from the previous article &quot;Extending the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 2 - Enabling JMX Monitoring on Hibernate v3 and Ehcache 1.3, on HibernateTutorialApp&quot; where we continue with (3) and (4) ...</summary>
<author>
<name>maxpoon</name>

<email>maxpoon@sun.com</email>
</author>
<dc:subject>Community: Java Enterprise</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/maxpoon/">
<![CDATA[<h2>Background</h2>
<p style="margin-bottom: 0in;">This is the continuation from the
previous article "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html">Extending
the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 2 - Enabling
JMX Monitoring on Hibernate v3 and Ehcache 1.3, on HibernateTutorialApp</a>"
where we continue with (3) and (4) of the following :<br>
</p>
<ol>
  <li><a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html#Ehcache-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to use Ehcache 1.3.0<br>
    </a></li>
  <li><a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html#JMX-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to enable JMX monitoring on
Hibernate and Ehcache<br>
    </a></li>
  <li><a href="#Configuring_SimpleJpaHibernateApp_to_use">Configuring
SimpleJpaHibernateApp to use Ehcache 1.3.0</a><br>
  </li>
  <li><a href="#Configuring_SimpleJpaHibernateApp_to_Hib">Configuring
SimpleJpaHibernateApp to enable JMX monitoring on
Hibernate and Ehcache</a></li>
</ol>
<br>
<h2><a name="Configuring_SimpleJpaHibernateApp_to_use"></a>(3)
Configuring
SimpleJpaHibernateApp to use Ehcache 1.3.0</h2>
The configurations for SimpleJpaHibernateApp, created
with the NetBeans tutorial "<a
 href="http://www.netbeans.org/kb/articles/hibernate-javaee.html">Using
Hibernate With the Java Persistence API</a>", to use Ehcache 1.3.0 are
similar to those of "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html#Ehcache-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to use Ehcache 1.3.0</a>" ,
except :<br>
<ul>
  <li>SimpleJpaHibernateApp is directly configured to use Hibernate
(Hibernate-3.2.2, or above, e.g.
Hibernate-3.2.4sp1) and Ehcache-1.3.0 </li>
  <li><span style="font-family: monospace;">persistence.xml </span>and<span
 style="font-family: monospace;"> hibernate.cfg.xml</span> (cache
related configurations&nbsp; highlighted in <span
 style="font-weight: bold; font-family: monospace;">bold</span>), and <span
 style="font-family: monospace;">ehcache.cfg.xml</span> as shown :</li>
</ul>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Code Listing 3.1 - persistence.xml for
SimpleJpaHibernateApp</span></td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version="1.0"
encoding="UTF-8"?&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;persistence
version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"<br>
&nbsp; &nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <br>
&nbsp; &nbsp; xsi:schemaLocation="http://java.sun.com/xml/ns/persistence<br>
&nbsp; &nbsp;
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &lt;persistence-unit
name="SimpleJpaHibernateAppPU" transaction-type="JTA"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;jta-data-source&gt;jdbc/sample&lt;/jta-data-source&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;class&gt;simpleJpaHibernateApp.entities.ProductCode&lt;/class&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;class&gt;simpleJpaHibernateApp.entities.Product&lt;/class&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;class&gt;simpleJpaHibernateApp.entities.Manufacturer&lt;/class&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;properties&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;property name=<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
"hibernate.ejb.classcache.simpleJpaHibernateApp.entities.ProductCode"<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; value="read-write"/&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;property name=<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"hibernate.ejb.classcache.simpleJpaHibernateApp.entities.Product"<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; value="read-write"/&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;property name=<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"hibernate.ejb.classcache.simpleJpaHibernateApp.entities.Manufacturer"<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value="read-write"/&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.ejb.cfgfile"
value="/hibernate.cfg.xml"/&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;/properties&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;
&lt;/persistence-unit&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;/persistence&gt;</span></td>
    </tr>
  </tbody>
</table>
<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Code Listing 3.2 - hibernate.cfg.xml for
SimpleJpaHibernateApp</span><br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version='1.0'
encoding='utf-8'?&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;!DOCTYPE
hibernate-configuration PUBLIC<br style="font-family: monospace;">
&nbsp; "-//Hibernate/Hibernate
Configuration DTD 3.0//EN"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;hibernate-configuration&gt;<br>
      <br style="font-family: monospace;">
      </span> <span style="font-family: monospace;">&nbsp;
&lt;session-factory&gt;</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;!--
SQL dialect --&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.dialect"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
org.hibernate.dialect.DerbyDialect&lt;/property&gt;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;!--
Debug logging of SQL statements --&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;!--
Cache Configurations --&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;!--
Using net.sf.ehcache.hibernate.SingletonEhCacheProvider instead of</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
net.sf.ehcache.hibernate.EhCacheProvider ensures the same instance<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; of CacheManager is referred to by
both Hibernate and our JMX Agent</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
simpleJpaHibernateApp.agents.jmxAgent. (Thanks to Greg Luck!)&nbsp;
--&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.cache.provider_class"&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
net.sf.ehcache.hibernate.SingletonEhCacheProvider&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.cache.provider_configuration"&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/ehcache.cfg.xml&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.use_minimal_puts"&gt;false&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.use_query_cache"&gt;true&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.use_structured_entries"&gt;true&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;/session-factory&gt;<br>
      <br style="font-family: monospace;">
      </span> <span style="font-family: monospace;">&lt;/hibernate-configuration&gt;</span><br>
      </td>
    </tr>
  </tbody>
</table>
<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Coding Listing 3.3 - ehcache.cfg.xml</span><span
 style="font-weight: bold;"> for
SimpleJpaHibernateApp</span> </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version="1.0"
encoding="UTF-8"?&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
xsi:noNamespaceSchemaLocation="ehcache.xsd"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &lt;diskStore
path="java.io.tmpdir"/&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &lt;defaultCache</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
maxElementsInMemory="10000"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
eternal="false"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
timeToIdleSeconds="120"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
timeToLiveSeconds="120"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
overflowToDisk="true"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
diskPersistent="true"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
diskExpiryThreadIntervalSeconds="120"</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
memoryStoreEvictionPolicy="LRU"</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; /&gt;<br>
&nbsp; &lt;cache name="simpleJpaHibernateApp.entities.ProductCode"<br>
&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxElementsInMemory="300"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eternal="true"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; overflowToDisk="false"<br>
&nbsp; /&gt;<br>
&nbsp; &lt;cache name="simpleJpaHibernateApp.entities.Product"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
maxElementsInMemory="300"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eternal="true"<br>
&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; overflowToDisk="false"<br>
&nbsp; /&gt;<br>
&nbsp; &lt;cache name="simpleJpaHibernateApp.entities.Manufacturer"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
maxElementsInMemory="300"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eternal="true"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; overflowToDisk="false"<br>
&nbsp; /&gt;<br>
&nbsp; &lt;cache
name="simpleJpaHibernateApp.entities.ProductCode.productCollection"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
maxElementsInMemory="300"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eternal="true"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; overflowToDisk="false"<br>
&nbsp; /&gt;<br>
&nbsp; &lt;cache
name="simpleJpaHibernateApp.entities.Manufacturer.productCollection"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
maxElementsInMemory="300"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eternal="true"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; overflowToDisk="false"<br>
&nbsp; /&gt;<br style="font-family: monospace;">
      </span> <span style="font-family: monospace;">&lt;/ehcache&gt;</span><br>
      </td>
    </tr>
  </tbody>
</table>
<h2><br>
</h2>
<h2><a name="Configuring_SimpleJpaHibernateApp_to_Hib"></a>(4)
Configuring SimpleJpaHibernateApp to enable JMX monitoring on
Hibernate and Ehcache</h2>
After being configured to use Ehcache 1.3.0 as shown in "<a
 href="#Configuring_SimpleJpaHibernateApp_to_use">Configuring
SimpleJpaHibernateApp to use Ehcache 1.3.0</a>" above, the
SimpleJpaHibernateApp can also be extended to
enable JMX monitoring, e.g. on Hibernate and Ehcache used, similar to "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html#JMX-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to enable JMX monitoring on
Hibernate and Ehcache</a>" in previous article.<br>
<br>
<h3>Step 4.1 - Create JMX Agent with Hibernate and Ehcache MBeans
Registration Codes</h3>
Similar to Step 2.1
in "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html#JMX-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to enable JMX monitoring on
Hibernate and Ehcache</a>", we need to create the JMX Agent which
registers the Hibernate
and Ehcache MBeans to enable JMX monitoring on them.&nbsp; However,
there are a few small but important differences here...<br>
<br>
(A) To register Hibernate's MBean, we still need the following codes :<br>
<br>
<span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
// Enable Hibernate JMX Statistics</span><br
 style="font-family: monospace;">
<span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
StatisticsService statsMBean = new StatisticsService();</span><br
 style="font-family: monospace;">
<span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
statsMBean.setSessionFactory(sessionFactory);</span><br
 style="font-family: monospace;">
<span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
statsMBean.setStatisticsEnabled(true);</span><br
 style="font-family: monospace;">
<span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
mbs.registerMBean(statsMBean, on);</span><br>
<br>
i.e. we need to get Hibernate <a
 href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/SessionFactory.html"
 style="font-family: monospace;">SessionFactory</a> from our JPA
execution environment (which uses the implementation-independent JPA <a
 href="http://java.sun.com/javaee/5/docs/api/javax/persistence/PersistenceUnit.html"
 style="font-family: monospace;">PersistentUnit</a> and the associated <a
 href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManagerFactory.html"
 style="font-family: monospace;">EntityMangerFactory</a> instead) to
enable JMX monitoring on Hibernate, by (also used by the NetBeans
Tutorial "<a
 href="http://wiki.netbeans.org/wiki/view/UsingHibernateWithJPA">NetBeans
Wiki - UsingHibernateWithJPA</a>") :<br>
<ul style="color: rgb(255, 0, 0);">
  <li><span style="font-family: monospace; font-weight: bold;">Session
session = (Session) em.getDelegate();</span></li>
</ul>
And, in our case for SimpleJpaHibernateApp, it is followed by :<br>
<ul style="color: rgb(255, 0, 0);">
  <li><span style="font-family: monospace; font-weight: bold;">SessionFactory
sessionFactory = session.getSessionFactory();</span></li>
</ul>
to get <a
 href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/SessionFactory.html"
 style="font-family: monospace;">SessionFactory</a> as in Code Listing
4.1 (additional MBeans registration codes in <span
 style="font-weight: bold; font-family: monospace;">bold</span>) below.<br>
<br>
(B) Also, we would like to modify the <a
 href="#simpleJpaHibernateApp.JmxAgent.init"><span
 style="font-family: monospace;">init()</span></a> method to take <a
 href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManagerFactory.html"
 style="font-family: monospace;">EntityMangerFactory</a> from the
calling context, to get the Hibernate <a
 href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/SessionFactory.html"
 style="font-family: monospace;">SessionFactory</a> for registering
Hibernate <a href="#simpleJpaHibernateApp.JmxAgent.statsMBean"><span
 style="font-family: monospace;">statsMBean</span></a>.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(192, 192, 192);"><span
 style="font-weight: bold;">Code Listing 4.1 - SimpleJpaHibernateApp's
JmxAgent.java</span><br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">/*</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* JmxAgent.java</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*/</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">package
simpleJpaHibernateApp.agents;</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">import
java.lang.management.ManagementFactory;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.management.MBeanServer;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.management.ObjectName;</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">import
javax.persistence.EntityManagerFactory;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">import
javax.persistence.EntityManager;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">import
net.sf.ehcache.CacheManager;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">import
net.sf.ehcache.management.ManagementService;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">import
org.hibernate.Session;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">import
org.hibernate.SessionFactory;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">import
org.hibernate.jmx.StatisticsService;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">/**</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* JMX agent
(singleton) for monitoring Hibernate and Ehcache</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* in
SimpleJpaHibernateApp, which uses:</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* &lt;ul&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*
&lt;li&gt;JavaServer Faces (JSF) web-tier&lt;/li&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* &lt;li&gt;Java
Persistence API (JPA) persistence&lt;/li&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* &lt;li&gt;Hibernate
Core (3.2.4 sp1) and Hibernate EntityManager (3.3.1)&lt;/li&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* &lt;li&gt;Ehcache
1.3.0&lt;/li&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* &lt;/ul&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* @author Max Poon
(maxpoon@dev.java.net)</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*/</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">public class JmxAgent {</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span
 style="font-family: monospace; font-weight: bold; color: rgb(255, 0, 0);">&nbsp;
private
EntityManager em;</span><br
 style="font-family: monospace; font-weight: bold; color: rgb(255, 0, 0);">
      <span style="font-family: monospace; font-weight: bold;">&nbsp; <span
 style="color: rgb(255, 0, 0);">private
Session session;</span></span><br
 style="font-family: monospace; font-weight: bold; color: rgb(255, 0, 0);">
      <span
 style="font-family: monospace; font-weight: bold; color: rgb(255, 0, 0);">&nbsp;
private
SessionFactory sf;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; /**</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp; *
Instantiate, register MBeans, enable Hibernate &amp; Ehcache JMX
Statistics</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp; *
@param emf javax.persistence.EntityManagerFactory to be passed in</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
from the invoking context (instead of creating it here</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
which is expensive operation)</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp; */</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;"><a
 name="simpleJpaHibernateApp.JmxAgent.init"></a>&nbsp; public
void <span style="font-weight: bold;">init(</span><span
 style="font-weight: bold; color: rgb(255, 0, 0);">EntityManagerFactory
emf</span><span style="font-weight: bold;">)</span>
throws Exception {</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
try {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Create EntityManager from EntityManagerFactory passed in </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// from the invoking context</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span
 style="color: rgb(255, 0, 0);">
em = emf.createEntityManager();</span></span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Get Hibernate Session and SessionFactory from EntityManager</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// *Important* for registering the Hibernate SessionFactory</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//&nbsp; with org.hibernate.jmx.StatisticsService later on</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//&nbsp; to enable JMX monitoring of Hibernate statistics</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <span style="color: rgb(255, 0, 0);">session = (Session)
em.getDelegate();</span></span><br
 style="font-family: monospace; font-weight: bold; color: rgb(255, 0, 0);">
      <span
 style="font-family: monospace; font-weight: bold; color: rgb(255, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
sf = session.getSessionFactory();</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp; &nbsp;
} catch (Exception ex) {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
ex.printStackTrace();</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
} finally {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
em.close();</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
}</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
ObjectName on = new ObjectName<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
("Hibernate:type=statistics,application=SimpleJpaHibernateApp");</span><br
 style="font-family: monospace; font-weight: bold;">
      <br style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
// Enable Hibernate JMX Statistics</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;"><a
 name="simpleJpaHibernateApp.JmxAgent.statsMBean"></a>&nbsp;&nbsp;&nbsp;
StatisticsService statsMBean = new StatisticsService();</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
statsMBean.setSessionFactory(sf);</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
statsMBean.setStatisticsEnabled(true);</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
mbs.registerMBean(statsMBean, on);</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; /*<br>
&nbsp; &nbsp;&nbsp; * Enable Ehcache JMX Statistics</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp; *
Use CacheManager.getInstance() instead of new CacheManager()</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
* as net.sf.ehcache.hibernate.SingletonEhCacheProvider is used</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
* to ensure reference to the same CacheManager instance as used</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
* by Hibernate</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
*/</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
CacheManager cacheMgr = CacheManager.getInstance();</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
ManagementService.registerMBeans<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
(cacheMgr, mbs, true, true, true, true);</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp; }</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; /**</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp; *
Returns an agent singleton.</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp; */</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; public
synchronized static JmxAgent getDefault(EntityManagerFactory emf)<br>
&nbsp;&nbsp;&nbsp; &nbsp; throws Exception {</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
if(singleton == null) {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
singleton = new JmxAgent();</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;
&nbsp; &nbsp;
singleton.init(<span style="color: rgb(255, 0, 0);">emf</span>);</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
}</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
return singleton;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; }</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; public
MBeanServer getMBeanServer() {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
return mbs;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; }</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; //
Platform MBeanServer used to register your MBeans</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; private
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; //
Singleton instance</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; private
static JmxAgent singleton;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">}</span><br>
      </td>
    </tr>
  </tbody>
</table>
<br>
<h3>Step 4.2 Modify JSF Managed Beans to initiate JMX Agent<br>
</h3>
<p style="margin-bottom: 0in;">Unlike the HibernateTravelPOJO
application, there
is no <span style="font-family: monospace;">HibernateUtil.java</span>-like
object for getting Hibernate <a
 href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/classic/Session.html"
 style="font-family: monospace;">Session</a> in
SimpleJpaHibernateApp.&nbsp; One solution is to initiate JmxAgent
during the 1st retrieval of <a
 href="http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html"
 style="font-family: monospace;">EntityManager</a> in following JSF
managed beans / controllers :<br>
</p>
<ul>
  <li><span style="font-family: monospace;">simpleJpaHibernate.controllers.ProductController</span></li>
  <li><span style="font-family: monospace;">simpleJpaHibernate.controllers.</span><span
 style="font-family: monospace;">ProductCodeController</span></li>
  <li><span style="font-family: monospace;">simpleJpaHibernate.controllers.</span><span
 style="font-family: monospace;">ManufacturerController</span></li>
</ul>
<p style="margin-bottom: 0in;">e.g. as shown in Code Listing 4.2 for <span
 style="font-family: monospace;">simpleJpaHibernate.controllers.ProductController</span>
:<br>
<br>
</p>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Code Listing 4.2 - ProductController.java
modified to initiate JmxAgent if needed<br>
      </span></td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">/*</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*
ProductController.java</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*/</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">package
simpleJpaHibernateApp.controller;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">import java.util.ArrayList;<br>
import java.util.Collection;<br>
import java.util.List;<br>
import javax.annotation.Resource;<br>
&nbsp;....</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">import
simpleJpaHibernateApp.agents.JmxAgent;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">import
simpleJpaHibernateApp.entities.Manufacturer;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">import
simpleJpaHibernateApp.entities.Product;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">import
simpleJpaHibernateApp.entities.ProductCode;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">/**<br>
&nbsp;* This version of ProductController uses JPA query with SQL joint<br>
&nbsp;* to retrieve all Products associated with a given ProductCode<br>
&nbsp;* indicated by its ID 'selectedProdCode'.<br>
&nbsp;* This shows simple code modification to get additional behaviour.<br>
&nbsp;*<br>
&nbsp;* @author Max Poon (maxpoon@dev.java.net)<br>
&nbsp;*/</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">public class
ProductController {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; /**
Creates a new instance of ProductController */</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; public
ProductController() { }</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
Product product;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
ProductCode selectedProductCode;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
String selectedProdCode;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
DataModel model;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; @Resource</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
UserTransaction utx;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
@PersistenceUnit(unitName = "SimpleJpaHibernateAppPU")</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
EntityManagerFactory emf;</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
EntityManager getEntityManager() {</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
initJMX();</span><br style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return emf.createEntityManager();</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; }</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; //
Initiate JMX if needed</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
void initJMX() {</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
try {</span><br style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
JmxAgent.getDefault(emf);</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
} catch (Exception ex) {</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
ex.printStackTrace();</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span><br style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
}<br style="font-family: monospace; font-weight: bold;">
      </span><span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
...</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; ...</span><br
 style="font-family: monospace;">
      <br>
      </td>
    </tr>
  </tbody>
</table>
<p style="margin-bottom: 0in;">Similar modifications should also be
done for :<br>
</p>
<ul>
  <li><span style="font-family: monospace;">simpleJpaHibernate.controllers.</span><span
 style="font-family: monospace;">ProductCodeController</span></li>
  <li><span style="font-family: monospace;">simpleJpaHibernate.controllers.</span><span
 style="font-family: monospace;">ManufacturerController</span></li>
</ul>
Then recompile SimpleJpaHibernateApp
with the above configurations and checked that it is working fine
getting also the web interface and functionalities as those in "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n.html">Extending
the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 1 -
Co-ordinating Query Views Based on Parameter Passing from JSF View to
Managed Bean</a>".<br>
<br>
<h3>Step 4.3 - Use
JConsole to Observe JMX Statistics</h3>
Similar to Step 2.3 in "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html#JMX-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to enable JMX monitoring on
Hibernate and Ehcache</a>", start <a
 href="http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html">JConsole</a>
and connect to JVM running GlassFish (indicated by
"<span style="font-family: monospace;">com.sun.enterprise.server.PELaunch</span>"
in JConsole) and observe the
following real-time JMX information collected.<br>
<br>
Starting with the "Listing ProductCodes" page :<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.3.1_Listing_ProductCode"></a>Figure
4.3.1 - "Listing ProductCodes" page of SimpleJpaHibernateApp<br>
      </span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/SimpleJpaHibernateApp-ProdCodeList0.png"
 title="" alt="" style="width: 650px; height: 476px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.3.2_JConsole_HibernateStats"></a>Figure
4.3.2 - JConsole showing Hibernate Entity Names, Queries, 2nd Level
Cache Regions, and related statistics</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.3.2_JConsole1_list-productCodes-1-hibstat.PNG"
 title="" alt="" style="width: 650px; height: 587px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a
 name="Fig4.3.3_JConsole_EhcacheStat_ProductCode"></a>Figure 4.3.3 -
JConsole showing Ehcache Statistics on ProductCode, with 6 cache miss
for the 6 ProductCode instances retrieved<br>
      </span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.3.3_JConsole1_list-productCodes-2-ehstat-productCodes.PNG"
 title="" alt="" style="width: 650px; height: 351px;"><br>
<br>
Click on "SW" page on "Listing ProductCodes" page to get the following,
then click on "List of Product with this Product Code" to get the next
screen shown in <a href="#Fig4.3.7_ListingProducts_ProdCodeSW">Figure
4.3.7</a>.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 4.3.4 - "Detail of ProductCode" page
of SimpleJpaHibernateApp for ProdCode="SW"<br>
      </span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/SimpleJpaHibernateApp-ProdCodeDetail1.png"
 title="" alt="" style="width: 650px; height: 476px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.3.5_HibernateStats"></a>Figure
4.3.5 - JConsole showing Hibernate Statistics being updated as compared
to <a href="#Fig4.3.2_JConsole_HibernateStats">Figure
4.3.2</a><br>
      </span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.3.5_JConsole2_detail-productCodes-SW-1-hibstat.PNG"
 title="" alt="" style="width: 650px; height: 587px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a
 name="Fig4.3.6_JConsole_EhcacheStat_ProductCode"></a>Figure 4.3.6 -
JConsole showing Ehcache Statistics for ProductCode updated (with 2
more cache misses and 7 more cache hits) as compared to <a
 href="#Fig4.3.3_JConsole_EhcacheStat_ProductCode">Figure
4.3.3</a><br>
      </span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.3.6_JConsole2_detail-productCodes-SW-2-ehstat.PNG"
 title="" alt="" style="width: 650px; height: 351px;"><br>
<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a
 name="Fig4.3.7_ListingProducts_ProdCodeSW"></a>Figure 4.3.7 - "Listing
Products" page of SimpleJpaHibernateApp for ProdCode="SW"</span></td>
    </tr>
  </tbody>
</table>
<img style="width: 650px; height: 476px;" alt="" title=""
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/SimpleJpaHibernateApp-SelectedProdList-SW.png"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.3.8_HibernateStats"></a>Figure
4.3.8 - JConsole showing Hibernate
Statistics being updated as compared to <a
 href="#Fig4.3.5_HibernateStats">Figure
4.3.5</a></span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.3.8_JConsole3_list-product-SW-1-hibstat.PNG"
 title="" alt="" style="width: 650px; height: 586px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.3.9_EhcacheStats_ProductCode"></a>Figure
4.3.9 - JConsole showing Ehcache
Statistics for ProductCode updated, with 1 more (ProductCode where
ProdCode="SW") cache hit as compared to <a
 href="#Fig4.3.6_JConsole_EhcacheStat_ProductCode">Figure
4.3.6</a></span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.3.9_JConsole3_list-product-SW-2-ehstat-productCodes.PNG"
 title="" alt="" style="width: 650px; height: 351px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.3.10_EhcacheStats_Product"></a>Figure
4.3.10 - JConsole showing Ehcache
Statistics for Product, with 8 new retrieval, i.e. cache misses, for
the 8 Product
instances where ProdCode="SW" as shown in <a
 href="#Fig4.3.7_ListingProducts_ProdCodeSW">Figure
4.3.7</a><br>
      </span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.3.10_JConsole3_list-product-SW-2-ehstat-product.PNG"
 title="" alt="" style="width: 650px; height: 351px;"><br>
<br>]]>
<![CDATA[<h3>Step 4.4 - Modify ProductController to Enable Collection Cache</h3>
The SimpleJpaHibernateApp is extended in "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n.html">Extending
the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 1 -
Co-ordinating Query Views Based on Parameter Passing from JSF View to
Managed Bean</a>" and the above steps, to demonstrate easy modification
to achieve JSF query views co-ordination and then JMX monitoring.&nbsp;
The ProductCode-to-Product entity 'navigation' and retrieval are done
by JPA Query with SQL joint and parameter binding as shown in Code
Listing
4.4.1, instead of one-to-many entity mapping and entity collection
retrieval.&nbsp; Hence, collection cache are not used as observed
previously from the
Hibernate JMX cache statistics on collection cache.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Coding Listing 4.4.1 -
simpleJpaHibernateApp.controllers.ProductController#getProducts() -
using JPA Query with SQL joint and parameter binding</span><br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">/**<br>
&nbsp;* This version of ProductController uses JPA query with SQL joint<br>
&nbsp;* to retrieve all Products associated with a given ProductCode<br>
&nbsp;* indicated by its ID 'selectedProdCode'.<br>
&nbsp;* This shows simple code modification to get additional behaviour.<br>
&nbsp;*<br>
&nbsp;* @author Max Poon (maxpoon@dev.java.net)<br>
&nbsp;*/<br>
public class ProductController {<br>
&nbsp; ...<br>
&nbsp; public
DataModel getProducts() {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
EntityManager em = getEntityManager();</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
try{</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Query q;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (selectedProdCode != null
&amp;&amp; selectedProdCode.length() != 0) {</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q = <span style="font-weight: bold;">em.createQuery("select object(o)
from Product as o " +</span></span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"where o.productCode.prodCode = :selectedProdCode")</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.setParameter("selectedProdCode", selectedProdCode);</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
} else {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q = em.createQuery("select object(o) from Product as o");</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q.setMaxResults(batchSize);</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q.setFirstResult(firstItem);</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
model = new ListDataModel(q.getResultList());</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;
return model;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
} finally {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
em.close();</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;
}</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; }<br>
      <br>
&nbsp; ...<br>
      </span><span style="font-family: monospace;"><br>
&nbsp; public int getItemCount() {<br>
&nbsp;&nbsp;&nbsp;&nbsp; EntityManager em =
getEntityManager();<br>
&nbsp;&nbsp;&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Query q;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (<span
 style="font-weight: bold;">selectedProdCode != null &amp;&amp;
selectedProdCode.length() != 0</span>) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q = <span style="font-weight: bold;">em.createQuery("select count(o)
from Product as o " +</span><br style="font-weight: bold;">
      <span style="font-weight: bold;">&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"where o.productCode.prodCode = :selectedProdCode")</span><br
 style="font-weight: bold;">
      <span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.setParameter("selectedProdCode", selectedProdCode);</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
else {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q = em.createQuery("select count(o) from Product as o");<br>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int
count = ((Long) q.getSingleResult()).intValue();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return count;<br>
&nbsp;&nbsp;&nbsp;&nbsp; } finally {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
em.close();<br>
&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp; }<br>
&nbsp; ...<br>
      </span></td>
    </tr>
  </tbody>
</table>
<br>
An alternative to the above is to get the <span
 style="font-family: monospace; font-weight: bold;">ProductCode</span>
entity class using the <span
 style="font-family: monospace; font-weight: bold;">selectedProdCode</span>
string :<br>
<ul>
  <li><span style="font-family: monospace; font-weight: bold;">ProductCode
productCode = em.find(ProductCode.class, selectedProdCode);</span></li>
</ul>
Then for <span style="font-family: monospace;">ProductController.getProducts()</span>,
retrieve all the associated <span
 style="font-family: monospace; font-weight: bold;">Product</span>'s
from <span style="font-family: monospace;">ProductCode.getProductonCollection()</span>
using one-to-many association :<br>
<ul>
  <li><span style="font-family: monospace; font-weight: bold;">productList
= (List&lt;Product&gt;) productCode.getProductCollection();</span></li>
</ul>
For <span style="font-family: monospace;">ProductController.getItemCount()</span>,<br>
<ul>
  <li><span style="font-family: monospace;"><span
 style="font-weight: bold;">count =
productCode.getProductCollection().size();</span></span></li>
</ul>
as shown in Code Listing 4.4.2 below :<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Coding Listing 4.4.2 -
simpleJpaHibernateApp.controllers.ProductController#getProducts() and
getItemCount() -
using JPA Entity Collection, e.g. to show use of Hibernate Collection
Cache in additional to Class Caching<br>
      </span></td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">/**<br>
&nbsp;* This version of ProductController uses entity collection for<br>
&nbsp;* retrieving all Products associated with a given ProductCode<br>
&nbsp;* indicated by its ID 'selectedProdCode' to show JMX monitoring<br>
&nbsp;* on collection cache in additional to entity class cache<br>
&nbsp;*<br>
&nbsp;* @author Max Poon (maxpoon@dev.java.net)<br>
&nbsp;*/<br>
public class ProductController {<br>
&nbsp; ...<br>
&nbsp; public DataModel getProducts() {</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
EntityManager em = getEntityManager();</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; try{</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &nbsp;
List&lt;Product&gt; productList;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if (selectedProdCode != null &amp;&amp; selectedProdCode.length() != 0)
{</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
ProductCode productCode = em.find(ProductCode.class, selectedProdCode);</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
productList = (List&lt;Product&gt;) productCode.getProductCollection();</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
} else {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Query q</span><span style="font-family: monospace;"> =
em.createQuery("select object(o) from Product as o");</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q.setMaxResults(batchSize);</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
q.setFirstResult(firstItem);</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
productList = (List&lt;Product&gt;) q.getResultList();</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
model = new ListDataModel(productList);</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return model;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; }
finally {</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
em.close();</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &nbsp; }</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; }<br>
&nbsp; ...<br>
&nbsp; public int getItemCount() {<br>
&nbsp;&nbsp;&nbsp; EntityManager em = getEntityManager();<br>
&nbsp;&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp; &nbsp; int count;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (selectedProdCode != null &amp;&amp;
selectedProdCode.length() != 0) {<br>
      <span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
ProductCode productCode = em.find(ProductCode.class, selectedProdCode);</span><br
 style="font-weight: bold;">
      <span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
count = productCode.getProductCollection().size();</span><br
 style="font-weight: bold;">
&nbsp;&nbsp; &nbsp;&nbsp; } else {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Query q =
em.createQuery("select count(o) from Product as o");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count = ((Long)
q.getSingleResult()).intValue();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return count;<br>
&nbsp;&nbsp;&nbsp; } finally {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; em.close();<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp; ...<br>
      </span></td>
    </tr>
  </tbody>
</table>
<br>
The entity class <span style="font-family: monospace;">ProductCode.java</span>
is also required to further enable collection cache on <span
 style="font-family: monospace;">ProductCode.productCollection</span>
(similarly for&nbsp;<span style="font-family: monospace;"> </span><span
 style="font-family: monospace;">Manufacturer.productCollection</span>
in <span style="font-family: monospace;">Manufacturer.java</span>) :<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Code Listing 4.4.3 - ProductCode.java with
Hibernate @Cache Annotation to enable Entity Class Cache on ProductCode
and Collection Cache on ProductCode.productionCollection</span><br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">/*</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* ProductCode.java</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*/</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">package
simpleJpaHibernateApp.entities;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">import java.io.Serializable;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">import java.util.Collection;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.persistence.CascadeType;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.persistence.Column;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.persistence.Entity;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">import javax.persistence.Id;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.persistence.NamedQueries;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.persistence.NamedQuery;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.persistence.OneToMany;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">import
javax.persistence.Table;<br>
      <span style="font-weight: bold;">import
org.hibernate.annotations.Cache;</span><br style="font-weight: bold;">
      <span style="font-weight: bold;">import
org.hibernate.annotations.CacheConcurrencyStrategy;</span><br>
      </span><br style="font-family: monospace;">
      <span style="font-family: monospace;">/**</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* Entity class
ProductCode, adding Hibernate Annotations to enable<br>
&nbsp;*&nbsp; Entity Class Cache on ProductCode, and<br>
&nbsp;*&nbsp; Collection Cache on ProductCode.</span><span
 style="font-family: monospace;"><span style="font-family: monospace;">productCollection</span></span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* </span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;* @author Max Poon
(maxpoon@dev.java.net)</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;*/</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">@Entity</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">@Table(name =
"PRODUCT_CODE")</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">@NamedQueries( {<br>
&nbsp; ......<br style="font-family: monospace;">
      </span><span style="font-family: monospace;"> } )</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;"><span
 style="font-weight: bold;">@Cache(usage =
CacheConcurrencyStrategy.READ_WRITE)</span><br>
public class ProductCode implements Serializable {<br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; @Id</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
@Column(name = "PROD_CODE", nullable = false)</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
String prodCode;</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
@Column(name = "DISCOUNT_CODE", nullable = false)</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
char discountCode;</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
@Column(name = "DESCRIPTION")</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
String description;</span><br style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "productCode")</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;<span
 style="font-weight: bold;"> @Cache(usage =
CacheConcurrencyStrategy.READ_WRITE)</span></span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; private
Collection&lt;Product&gt; productCollection;</span></span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; ......</span><br>
      <br>
      </td>
    </tr>
  </tbody>
</table>
<br>
After recompiling SimpleJpaHibernateApp and invoking its "Listing
Products" page for <span style="font-family: monospace;">ProdCode="SW"</span>
(<a href="#Fig4.3.7_ListingProducts_ProdCodeSW">Figure
4.3.7</a>), the
JMX statistics collected by JConsole are shown :<br>
<br>
* Hibernate JMX statistics in <a href="#Fig4.4.1_HibernateStats">Figure
4.4.1</a> <br>
<br>
* Ehcache JMX statistics<br>
<ul>
  <li>Class Cache for <span style="font-family: monospace;">simpleJpaHibernateApp.entities.ProductCode</span>,
in <a href="#Fig4.4.2_EhcacheStats_ProductCode">Figure
4.4.2</a> <br>
  </li>
  <li>Class Cache for <span style="font-family: monospace;">simpleJpaHibernateApp.entities.Product</span>,
in <a href="#Fig4.4.3_EhcacheStat_Product">Figure
4.4.3</a> <br>
  </li>
  <li>Collection Cache <span style="font-family: monospace;">simpleJpaHibernateApp.entities.ProductCode.productionCollection</span>,
in <a href="#Fig4.4.4_EhcacheStats_ProductCode-productionCollection">Figure
4.4.4</a> <br>
  </li>
</ul>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.4.1_HibernateStats"></a>Figure
4.4.1 - JConsole showing Hibernate JMX statistics, on
SimpleJpaHibernateApp using @Cache Annotation (to enable Collection
Cache), as compared to <a href="#Fig4.3.8_HibernateStats">Figure
4.3.8</a>
for SimpleJpaHibernateApp using JPA Query</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.4.1_JConsole3_list-product-SW-1-hibstat.PNG"
 title="" alt="" style="width: 650px; height: 487px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.4.2_EhcacheStats_ProductCode"></a>Figure
4.4.2 - JConsole showing Ehcache JMX Class Cache Statistics for
ProductCode, on SimpleJpaHibernateApp using @Cache
Annotation to enable Collection Cache</span><span
 style="font-weight: bold;">, as compared to <a
 href="#Fig4.3.9_EhcacheStats_ProductCode">Figure
4.3.9</a> for
SimpleJpaHibernateApp using JPA Query</span></td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.4.2_JConsole3_list-product-SW-2-ehstat-productCodes.PNG"
 title="" alt="" style="width: 650px; height: 369px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Fig4.4.3_EhcacheStat_Product"></a>Figure
4.4.3 - JConsole showing Ehcache JMX Class Cache Statistics for
Product, on SimpleJpaHibernateApp using @Cache
Annotation to enable Collection Cache</span><span
 style="font-weight: bold;">, as compared to <a
 href="#Fig4.3.10_EhcacheStats_Product">Figure
4.3.10</a> for
SimpleJpaHibernateApp using JPA Query</span> </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.4.3_JConsole3_list-product-SW-2-ehstat-product.PNG"
 title="" alt="" style="width: 650px; height: 369px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a
 name="Fig4.4.4_EhcacheStats_ProductCode-productionCollection"></a>Figure
4.4.4 - JConsole showing Ehcache JMX Collection Cache Statistics for
ProductCode.productCollection, on SimpleJpaHibernateApp using @Cache
Annotation to enable Collection Cache (Collection Cache not used in
SimpleJpaHibernateApp using JPA Query)</span> </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.4.4_JConsole3_list-product-SW-2-ehstat-productCodes-products.PNG"
 title="" alt="" style="width: 650px; height: 369px;"><br>
<br>
Resulting from the above JMX statistics collected on Hibernate and
Ehcache, an indicative comparison of cache and query statistics for the
same use case can be tabulated, e.g. for the mentioned case of querying
all the 6 <span style="font-family: monospace; font-weight: bold;">Product</span>'s
associated with <span
 style="font-family: monospace; font-weight: bold;">ProductCode</span>
where <span style="font-family: monospace;">ProdCode="SW"</span>,
showing the following major observations :<br>
<ul>
  <li>Querying using entity collection causes slightly more entity
loading (28 vs 29) but, at the same time, more utilisation of entity
class cache and Hibernate 2nd level cache</li>
  <ul>
    <li>(<span style="font-family: monospace;">2ndLevelCacheHit</span>
of 2 vs 109, for the respective cases)<br>
    </li>
  </ul>
  <li>Querying using entity collection improves query by :<br>
  </li>
  <ul>
    <li>requiring more simple query (compare the different <span
 style="font-family: monospace;">QueryExecutionMaxTimeQueryString</span>'s)<br>
    </li>
  </ul>
  <ul>
    <li>reducing time of the longest query execution (<span
 style="font-family: monospace;">QueryExecutionMaxTime </span>from 181
ms to 60 ms)<br>
    </li>
    <li>reducing number of queries required (<span
 style="font-family: monospace;">QueryExecutionCount</span> from 27 to
20)<br>
    </li>
  </ul>
</ul>
<br>
<table style="width: 650px; text-align: left;" border="1"
 cellspacing="2" cellpadding="2">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(153, 153, 153);"><br>
      </td>
      <td
 style="vertical-align: top; text-align: center; font-weight: bold; background-color: rgb(204, 204, 204); width: 33%;">Using
JPA Query<br>
with SQL Joint<br>
      </td>
      <td
 style="vertical-align: top; text-align: center; font-weight: bold; background-color: rgb(204, 204, 204); width: 30%;">Using
Entity Collection <br>
&amp; Collection Cache<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Hibernate JMX Statistics</span><br>
      </td>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><br>
      </td>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">Entity
Load Count<br>
      </td>
      <td style="vertical-align: top; text-align: center; width: 33%;">28<br>
      </td>
      <td style="vertical-align: top; text-align: center; width: 30%;">29<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">2nd
Level Cache Hit<br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 33%; font-weight: bold;">2<br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 30%; font-weight: bold;">109<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">2nd
Level Cache Miss<br>
      </td>
      <td style="vertical-align: top; text-align: center; width: 33%;">1<br>
      </td>
      <td style="vertical-align: top; text-align: center; width: 30%;">2<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">2nd
Level Cache Put<br>
      </td>
      <td style="vertical-align: top; text-align: center; width: 33%;">28<br>
      </td>
      <td style="vertical-align: top; text-align: center; width: 30%;">30<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">Query
Execution Count<br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 33%; font-weight: bold;">27<br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 30%; font-weight: bold;">20<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">Query
Execution Max Time (ms)<br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 33%; font-weight: bold;">181<br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 30%; font-weight: bold;">60<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">Query
Execution Max Time Query String<br>
      </td>
      <td style="vertical-align: top; text-align: left; width: 33%;"><span
 style="font-family: monospace;">select count(o)</span><br>
      <span style="font-family: monospace;">from Product as o </span><span
 style="font-family: monospace;">where o.productCode.prodCode<br>
= :selectProdCode;</span><br>
      </td>
      <td style="vertical-align: top; text-align: left; width: 30%;"><span
 style="font-family: monospace;">select object(o)</span><br>
      <span style="font-family: monospace;">from Product as o;</span><br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Ehcache JMX Statistics</span><br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 33%; background-color: rgb(204, 204, 204);"><br>
      </td>
      <td
 style="vertical-align: top; text-align: center; width: 30%; background-color: rgb(204, 204, 204);"><br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">ProductCode
Cache Hit / Miss</td>
      <td style="vertical-align: top; text-align: center;">3 / 13<br>
      </td>
      <td style="vertical-align: top; text-align: center;">8 / 16<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">Product
Cache Hit / Miss<br>
      </td>
      <td style="vertical-align: top; text-align: center;">0 / 8<br>
      </td>
      <td style="vertical-align: top; text-align: center;">48 / 8<br>
      </td>
    </tr>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">ProductCode.productCollection
Cache Hit / Miss<br>
      </td>
      <td style="vertical-align: top; text-align: center;">Not Available<br>
      </td>
      <td style="vertical-align: top; text-align: center;">6 / 2<br>
      </td>
    </tr>
  </tbody>
</table>
<br>
<span style="font-weight: bold;">Notes:</span><br>
<ul>
  <li>Relative values of <span style="font-family: monospace;">QueryExecutionMaxTime</span>
may differ slightly in different testing
environments and value above are just the results collected during my
testings.</li>
  <li>The new structure of the SimpleJpaHibernateApp application shown
as NetBeans Projects is included for reference :</li>
</ul>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 4.4.5 - New Structure of
SimpleJpaHibernateApp NetBeans Project</span><br>
      </td>
    </tr>
    <tr align="center">
      <td style="vertical-align: top;"><img
 src="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/4.4.5_simpleJpaHibernateApp_newStructure.PNG"
 title="" alt="" style="width: 379px; height: 600px;"><br>
      </td>
    </tr>
  </tbody>
</table>
<br>
<h2>Summary</h2>
To summarise, the above shows :<br>
<ul>
  <li>ways of extending JSF-Hibernate applications to enable JMX
monitoring on its Java object/relational persistence (Hibernate v3) and
cache (Ehcache 1.3.0) implementations, for both Hibernate v3 and
JPA-with-Hibernate applications</li>
</ul>
<ul>
  <li>an example (using SimpleJpaHibernateApp) of how JMX statistics
enable comparison of query execution and cache utilisation for
application providing same functionalities but using different
implementation strategies </li>
</ul>
<span style="font-weight: bold;">Note</span>: While only JMX statistics
for Java object/relational persistence (Hibernate v3) and cache
(Ehcache 1.3.0) implementations are discussed above,
application-specific JMX statistics can also be gathered by building
custom JMX MBeans, e.g. as shown in the NetBeans tutorial "<a
 href="http://www.netbeans.org/kb/articles/jmx-getstart.html">Getting
Started with JMX Monitoring in NetBeans IDE 5.0</a>".<br>
<br>
<h2>Acknowledgements</h2>
Special thanks to <a href="http://gregluck.com/blog/">Greg Luck</a>
for his collaboration in the configuration of HibernateTravelPOJO and
SimpleJpaHibernateApp to enable JMX monitoring on <a
 href="http://ehcache.sourceforge.net/">Ehcache</a> 1.3.0&nbsp;
released
earlier in June 11, 2007.<br>
<br>
<h2>Resources</h2>
<ul>
  <li><a
 href="http://www.hibernate.org/hib_docs/core/reference/en/html/performance.html">Hibernate
v3 Reference Chapter 19 : Improving Performance</a> <br>
  </li>
  <li><a href="http://www.devx.com/dbzone/Article/29685/1954?pf=true">Speed
Up Your Hibernate Applications with Second-Level Caching</a></li>
  <li><a href="http://www.hibernate.org/216.html">Hibernate :
Publishing statistics through JMX</a> </li>
  <li><a
 href="http://www.awprofessional.com/articles/article.asp?p=368645&amp;seqNum=5&amp;rl=1">Gathering
Performance Metrics for Hibernate</a> </li>
  <li><a href="http://ehcache.sourceforge.net/documentation/jmx.html">Ehcache
User Guide : JMX Management and Monitoring</a> <br>
  </li>
  <li>Zip archive of the resulting extended "<a
 href="http://weblogs.java.net/blog/maxpoon/HibernateTravelPOJO/HibernateTravelPOJO.zip">HibernateTravelPOJO</a>"
and
"<a
 href="http://weblogs.java.net/blog/maxpoon/HibernateTutorialApp/HibernateTutorialApp.zip">HibernateTutorialApp</a>"
NetBeans projects / source packages</li>
  <li>Zip archive of the resulting extended "<a
 href="http://weblogs.java.net/blog/maxpoon/SimpleJpaHibernateApp/SimpleJpaHibernateApp.zip">SimpleJpaHibernateApp</a>"
NetBeans project / source package</li>
</ul>
<span style="font-weight: bold;">Note:</span> All the 3 application
source packages can either be opened and compiled in <a
 href="http://www.netbeans.org/products/ide/">NetBeans</a><a
 href="http://www.netbeans.org/products/ide/"> 5.5</a> or above, or
compiled by <a href="http://ant.apache.org/bindownload.cgi">Ant 1.6.5</a> or above.<br>]]>
</content>
</entry>
<entry>
<title>Extending the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 2 - Enabling JMX Monitoring on Hibernate v3 and Ehcache 1.3.0 on &quot;HibernateTutorialApp&quot;</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n_2.html" />
<modified>2007-08-12T18:47:19Z</modified>
<issued>2007-06-27T17:12:47Z</issued>
<id>tag:weblogs.java.net,2007:/blog/maxpoon/406.7744</id>
<created>2007-06-27T17:12:47Z</created>
<summary type="text/plain">Apart from showing quite an example of easy enhancement of JSF-based web application as discussed earlier in &quot;Extending the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 1 - Co-ordinating Query Views Based on Parameter Passing from JSF View to Managed Bean&quot;, the NetBeans tutorial JSF-JPA-Hibernate application created (&apos;SimpleJpaHibernateApp&apos;) can also be easily extended to demonstrate JMX monitoring on Hibernate v3 and the latest Ehcache 1.3.0 - the respective very common open source Java object/relational persistence and caching implementations, which have also included support for JMX to enable monitoring on their operational states and statistics, including the overall (e.g. query and 2nd level cache) cache hit/miss statistics, time of the slowest query (from Hibernate v3), and individual cache hit/miss statistics (from Ehcache 1.3.0).</summary>
<author>
<name>maxpoon</name>

<email>maxpoon@sun.com</email>
</author>
<dc:subject>J2EE</dc:subject>
<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/maxpoon/">
<![CDATA[<h2>Background<br>
</h2>
<p style="margin-bottom: 0in;">Apart from showing quite an
example of easy enhancement of JSF-based web application as discussed
earlier in "<a
 href="http://weblogs.java.net/blog/maxpoon/archive/2007/06/extending_the_n.html">Extending
the NetBeans Tutorial JSF-JPA-Hibernate Application, Part 1 -
Co-ordinating Query Views Based on Parameter Passing from JSF View to
Managed Bean</a>", the NetBeans tutorial JSF-JPA-Hibernate application
created ('SimpleJpaHibernateApp') can also be easily extended to
demonstrate <a
 href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/">JMX</a>
monitoring on <a href="http://www.hibernate.org">Hibernate</a> v3 and
the latest <a href="http://ehcache.sourceforge.net/">Ehcache</a> 1.3.0
-
the respective very common open
source Java object/relational persistence and caching implementations,
which have
also included support for <a
 href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/">JMX</a>
to enable monitoring on their
operational states and statistics, including the overall (e.g. query
and 2nd level cache) cache hit/miss
statistics, time of the slowest query (from <a
 href="http://www.hibernate.org">Hibernate</a>
v3), and
individual cache hit/miss statistics (from <a
 href="http://ehcache.sourceforge.net/">Ehcache</a> 1.3.0).<br><br>
</p>
<p style="margin-bottom: 0in;">These
monitoring capabilities give
very helpful information for application developers and administrators
to understand actual behaviour of their applications deployed, for
resource usage optimisation and performance tuning.<br>
</p>
<p style="margin-bottom: 0in;">The following shows the configurations
to use Ehcache 1.3.0, followed by configurations of JMX monitoring on
Hibernate v3 and Ehcache 1.3.0, for both :<br>
</p>
<ol>
  <li>Hibernate Core v3 applications<br>
  </li>
  <ul>
    <li>using the sample application ('HibernateTutorialApp')
created with the NetBeans tutorials "<a
 href="http://www.netbeans.org/kb/55/vwp-hibernate.html">Using
Hibernate With the NetBeans Visual Web Pack</a>" (previously as "<a
 href="http://developers.sun.com/jscreator/learning/tutorials/2/hibernate.html">Using
Hibernate With the Sun Java Studio Creator IDE</a>") </li>
  </ul>
  <li>JPA-with-Hibernate applications<br>
  </li>
  <ul>
    <li>using the sample application ('SimpleJpaHibernateApp') created
with the NetBeans tutorial "<a
 href="http://www.netbeans.org/kb/articles/hibernate-javaee.html">Using
Hibernate With the Java Persistence API</a>"</li>
  </ul>
</ol>
<p style="margin-bottom: 0in;">Since JMX monitoring configuration :<br>
</p>
<ul>
  <li>on Ehcache requires the latest Ehcache 1.3.0</li>
  <li>on
JPA-with-Hibernate application is also based on the configuration for
Hibernate Core
v3 application</li>
</ul>
<p style="margin-bottom: 0in;">we shall proceed in the following
sequence :<br>
</p>
<ul>
</ul>
<ol>
  <li><a href="#Ehcache-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to use Ehcache 1.3.0<br>
    </a></li>
  <li><a href="#JMX-for-HibernateTravelPOJO">Configuring
HibernateTutorialApp/HibernateTravelPOJO to enable JMX monitoring on
Hibernate and Ehcache<br>
    </a></li>
  <li>Configuring
SimpleJpaHibernateApp to use Ehcache 1.3.0 <br>
  </li>
  <li>Configuring
SimpleJpaHibernateApp to enable JMX monitoring on
Hibernate and Ehcache</li>
</ol>
and (3) and (4) above will be discussed in next article "Extending the
NetBeans Tutorial JSF-JPA-Hibernate Application, Part 3 - Enabling JMX
Monitoring on Hibernate v3 and Ehcache 1.3, on SimpleJpaHibernateApp".<br>
<br>
<h2><span style="font-weight: bold;">Software Requirements</span></h2>
<ul>
  <li>Deployment on <a href="http://java.sun.com/j2ee/1.4/index.jsp">J2EE
1.4</a> or above compliant application server
(e.g. <a
 href="http://www.sun.com/software/products/appsrvr/previous/pe8/index.xml">Sun
Java System Application Server 8.2</a>, <a
 href="https://glassfish.dev.java.net/downloads/v1_ur1-p01-b02.html">GlassFish
v1</a>, or above)
running <a href="http://java.sun.com/javase/downloads/index_jdk5.jsp">Sun
JDK 5</a> or above (<a href="http://java.sun.com/javase/6/">Sun JDK 6</a>
suggested) is required for HibernateTutorialApp/HibernateTravelPOJO to
have JMX monitoring.</li>
  <li>Deployment on <a
 href="http://java.sun.com/javaee/technologies/javaee5.jsp">Java EE 5</a>
compliant application server (e.g. <a
 href="https://glassfish.dev.java.net/downloads/v1_ur1-p01-b02.html">GlassFish
v1</a> or above) running <a
 href="http://java.sun.com/javase/downloads/index_jdk5.jsp">Sun JDK 5</a>
or above (<a href="http://java.sun.com/javase/6/">Sun JDK 6</a>
suggested) is
required for SimpleJpaHibernateApp to have JMX monitoring.</li>
  <li>The steps below assume use of <a
 href="http://www.netbeans.org/products/ide/">NetBeans</a><a
 href="http://www.netbeans.org/products/ide/"> 5.5 / 5.5.1</a>, <a
 href="https://glassfish.dev.java.net/downloads/v1_ur1-p01-b02.html">GlassFish
v1</a>,
and on <a href="http://java.sun.com/javase/6/">Sun JDK 6</a> or above
to allow <a
 href="http://weblogs.java.net/blog/mandychung/archive/2006/12/java_se_6_monit_1.html">JMX
Dynamic Attach</a> from <a
 href="http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html">JConsole</a>
to
the <a href="http://java.sun.com/javase/6/">Sun JDK 6</a> JVM running
GlassFish for JMX
monitoring.</li>
</ul>
<br>
<ul>
</ul>
<h2><a name="Ehcache-for-HibernateTravelPOJO"></a>(1) Configuring
HibernateTutorialApp/HibernateTravelPOJO to use Ehcache 1.3.0<br>
</h2>
As HibernateTutorialApp actually uses another application
'HibernateTravelPOJO'
supplied (in zip archive <a
 href="http://developers.sun.com/jscreator/learning/tutorials/2/hibernate/HibernateTravelPOJO.zip">HibernateTravelPOJO.zip</a>)
in the tutorial for its persistence POJOs, configuration to use Ehcache
1.3.0 (instead of any earlier Ehcache version bundled with Hibernate)
should be done in HibernateTravelPOJO.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr style="font-weight: bold;">
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);">Figure
1.0 - <span class="dkcaption1">Contents of the HibernateTravelPOJO
Project</span></td>
    </tr>
    <tr align="center">
      <td style="vertical-align: top;"><a
 href="http://developers.sun.com/jscreator/learning/tutorials/2/hibernate/hibernate_source_pkg.gif"><img
 src="http://weblogs.java.net/blog/maxpoon/HibernateTravelPOJO/1.0_hibernatetravelpojo_source_pkg.gif"
 title="" alt=""
 style="border: 0px solid ; width: 211px; height: 258px;"></a><a
 href="http://developers.sun.com/jscreator/learning/tutorials/2/hibernate/hibernate_source_pkg.gif"><br>
      </a></td>
    </tr>
  </tbody>
</table>
<br>
There are 2 cases for the configuration of HibernateTravelPOJO to use
Ehcache 1.3.0 :<br>
<br>
Case 1 : If you have already set up HibernateTravelPOJO and
HibernateTutorialApp according to the NetBeans
tutorial "<a href="http://www.netbeans.org/kb/55/vwp-hibernate.html">Using
Hibernate With the NetBeans Visual Web Pack</a>", you may just need to
replace any previous version of the <span
 style="font-family: monospace;">ehcache-*.jar</span> file used (as
specified in the Netbeans "Hibernate322" Class Library in the tutorial)
with the <span style="font-family: monospace;">ehcache-1.3.0.jar</span>
from Ehcache1.3.0 downloaded, then skip to <a
 href="#Step_1.4_-_Configure_Hibernate_to_use">Step 1.4</a> below.<br>
<br>
Case 2 : If you are just going to set up HibernateTravelPOJO and
HibernateTutorialApp, you can start with <a
 href="#Step_1.1_-_Create_Hibernate_Class_Library_">Step 1.1</a> below
to
set up HibernateTravelPOJO and HibernateTutorialApp to use the current
latest Hibernate 3.2.4sp1 and Ehcache 1.3.0.<br>
<br>
<h3><a name="Step_1.1_-_Create_Hibernate_Class_Library_"></a>Step 1.1 -
Create Hibernate Class Library in NetBeans IDE<br>
</h3>
First, follow the original NetBeans
tutorial "<a href="http://www.netbeans.org/kb/55/vwp-hibernate.html">Using
Hibernate With the NetBeans Visual Web Pack</a>" section "<a
 href="http://www.netbeans.org/kb/55/vwp-hibernate.html#04">Create a
Hibernate Class Library</a>" to create Class Library for Hibernate in
NetBeans IDE.&nbsp; Since the latest Hibernate 3.2.4.sp1 is
available (as at time of writing this article), it can be used in place
of Hibernate 3.2.2 suggested in the tutorial, also referring to <a
 href="#Fig1.1_Hibernate3.2.4sp1_ClassLibrary">Figure 1.1</a> below for
the list of jar files to be included for the NetBeans Class
Library "Hibernate3.2.4sp1".<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 648px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a
 name="Fig1.1_Hibernate3.2.4sp1_ClassLibrary"></a>Figure 1.1 - Create
HibernateCore3.2.4sp1
Class Library in NetBeans IDE</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/HibernateTravelPOJO/1.1_createHibernateLib.PNG"
 title="" alt="" style="width: 648px; height: 478px;"><br>
<br>
<h3>Step 1.2 - Create "Ehcache-1.3.0" Class Library&nbsp; in NetBeans
IDE</h3>
Download the latest Ehcache 1.3.0 (now including JMX support) from <a
 href="http://sourceforge.net/project/showfiles.php?group_id=93232&amp;package_id=98840&amp;release_id=515169">here</a>,
unzip it, and create new NetBeans Class Library "Ehcache-1.3.0" as
shown:<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 1.2.1 - Create new "Ehcache-1.3.0"
Class Library for NetBeans IDE</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/HibernateTravelPOJO/1.2.1_createEhcacheLib.PNG"
 title="" alt="" style="width: 648px; height: 478px;"><br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;">Figure 1.2.2 - Add ehcache-1.3.0.jar to the
new "Ehcache-1.3.0" Class Library</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/HibernateTravelPOJO/1.2.2_createEhcacheLib.PNG"
 title="" alt="" style="width: 648px; height: 478px;"><br>
<br>
<h3>Step 1.3 - Configure HibernateTravelPOJO to use Ehcache 1.3.0</h3>
Follow the steps of "<a
 href="http://www.netbeans.org/kb/55/vwp-hibernate.html#pojo">Using
Hibernate With the NetBeans Visual Web Pack</a>" to set it up, but with
the our new NetBeans Class Libraries "HibernateCore3.2.4sp1" and
"Ehcache-1.3.0" as follows.<br>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204); width: 647px;"><span
 style="font-weight: bold;">Figure 1.3 - Add Library "Ehcache-1.3.0" to
HibernateTravelPOJO</span><br>
      </td>
    </tr>
  </tbody>
</table>
<img
 src="http://weblogs.java.net/blog/maxpoon/HibernateTravelPOJO/1.3_AddHibEhcacheLib.PNG"
 title="" alt="" style="width: 647px; height: 479px;"><br>
<br>
<h3><a name="Step_1.4_-_Configure_Hibernate_to_use"></a>Step 1.4 -
Configure Hibernate to use Ehcache 1.3.0<br>
</h3>
To configure Hibernate v3 to use Ehcache 1.3.0, we shall start with
modifying the Hibernate configuration file <span
 style="font-family: monospace;">hibernate.cfg.xml</span> as shown in <a
 href="#Code1.4.1_HibernateTravelPOJO_hibernate.cfg.xml">Code
Listing 1.4.1</a> (additional settings required in<span
 style="font-family: monospace; font-weight: bold;"> bold</span>) to
enable Hibernate 2nd-level caching using Ehcache on the following
classes and collection for HibernateTravelPOJO :<br>
<ul>
  <li>Class <span style="font-family: monospace;"><span
 style="font-weight: bold;">hibernatetravelpojo.Person</span> </span>with
read-write mode</li>
  <li>Class <span style="font-family: monospace; font-weight: bold;">hibernatetravelpojo.Trip
    </span>with
read-write mode</li>
  <li>Collection<span style="font-family: monospace;"> <span
 style="font-weight: bold;">hibernatetravelpojo.Person.trips</span> </span>with
read-write mode</li>
</ul>
Meanwhile, we also need to modify the Hibernate mapping files <br>
<ul>
  <li><span style="font-family: monospace;">hibernatetravelpojo/<span
 style="font-weight: bold;">Person.hbm.xml</span></span></li>
  <li><span style="font-family: monospace;">hibernatetravelpojo/<span
 style="font-weight: bold;">Trip.hbm.xml</span></span></li>
</ul>
as shown in <a href="#Code1.4.2_Amended_Person.hbm.xml">Code Listing
1.4.2</a> and <a href="#Code1.4.3_Amended_Trip.hbm.xml">Code Listing
1.4.3</a> (again, additional settings
required in <span style="font-weight: bold;">bold</span>).<span
 style="font-family: monospace;"><br>
<br>
</span>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a
 name="Code1.4.1_HibernateTravelPOJO_hibernate.cfg.xml"></a>Code
Listing 1.4.1 - Modified hibernate.cfg.xml for HibernateTravelPOJO<br>
      </span> </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version='1.0'
encoding='utf-8'?&gt;<br>
&lt;!DOCTYPE
hibernate-configuration PUBLIC<br>
&nbsp; "-//Hibernate/Hibernate
Configuration DTD 3.0//EN"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&lt;hibernate-configuration&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;"><br>
&nbsp;
&lt;session-factory&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;"><br>
&nbsp;&nbsp;&nbsp; &lt;!-- Data Source Name --&gt;<br>
&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.connection.driver_class"&gt;<br>
&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;
org.apache.derby.jdbc.ClientDriver&lt;/property&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.connection.url"&gt;<br>
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
jdbc:derby://localhost:1527/travel&lt;/property&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.connection.username"&gt;travel&lt;/property&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.connection.password"&gt;travel&lt;/property&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;"><br>
&nbsp;&nbsp;&nbsp; &lt;!-- SQL dialect --&gt;<br>
&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.dialect"&gt;<br>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
org.hibernate.dialect.DerbyDialect&lt;/property&gt;</span><br
 style="font-family: monospace;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;!--
Debug logging of SQL statements --&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;property
name="hibernate.show_sql"&gt;true&lt;/property&gt;<br>
      <br>
&nbsp;&nbsp;&nbsp; &lt;!-- Enable Hibernate's automatic session context
management --&gt;<br>
&nbsp;&nbsp;&nbsp; &lt;property
name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;<br>
      <br>
&nbsp;&nbsp;&nbsp; &lt;!-- Enable Hibernate statistics generation --&gt;<br>
      <span style="font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.generate_statistics"&gt;true&lt;/property&gt;</span></span><br
 style="font-family: monospace; font-weight: bold;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;!--
Cache Configurations --&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp; &lt;!--
Using net.sf.ehcache.hibernate.SingletonEhCacheProvider instead of</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
net.sf.ehcache.hibernate.EhCacheProvider ensures the same instance<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; of
CacheManager is referred to by both Hibernate and our JMX Agent</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
simpleJpaHibernateApp.agents.jmxAgent. (Thanks to Greg Luck!)&nbsp;
--&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property name="hibernate.cache.provider_class"&gt;<br>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
net.sf.ehcache.hibernate.SingletonEhCacheProvider&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.provider_configuration"&gt;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; /ehcache.cfg.xml&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;"><span
 style="font-weight: bold;">&nbsp;&nbsp;&nbsp; &lt;property
name="hibernate.cache.use_minimal_puts"&gt;false&lt;/property&gt;</span><br
 style="font-weight: bold;">
      </span><span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.use_query_cache"&gt;true&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;&nbsp;&nbsp;
&lt;property
name="hibernate.cache.use_structured_entries"&gt;true&lt;/property&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;!-- Mapping Files --&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;mapping resource="hibernatetravelpojo/Person.hbm.xml"/&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;mapping resource="hibernatetravelpojo/Trip.hbm.xml"/&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;
&lt;/session-factory&gt;<br>
      <br>
&lt;/hibernate-configuration&gt;</span><br>
      </td>
    </tr>
  </tbody>
</table>
<br>
<table cellpadding="2" cellspacing="2" border="1"
 style="text-align: left; width: 650px;">
  <tbody>
    <tr>
      <td
 style="vertical-align: top; background-color: rgb(204, 204, 204);"><span
 style="font-weight: bold;"><a name="Code1.4.2_Amended_Person.hbm.xml"></a>Coding
Listing 1.4.2 - Modified
Person.hbm.xml</span><br>
      </td>
    </tr>
    <tr>
      <td style="vertical-align: top;"><span
 style="font-family: monospace;">&lt;?xml version="1.0"
encoding="UTF-8"?&gt;<br>
&lt;!DOCTYPE hibernate-mapping PUBLIC<br>
&nbsp; "-//Hibernate/Hibernate Mapping DTD 3.0//EN"<br>
&nbsp; "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;<br>
      <br>
&lt;hibernate-mapping&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;"><br>
&nbsp; &lt;class name="hibernatetravelpojo.Person" table="PERSON"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;"><br>
&nbsp; &nbsp;
&lt;!-- Class Cache --&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace; font-weight: bold;">&nbsp;
&nbsp;
&lt;cache usage="read-write"/&gt;</span><br
 style="font-family: monospace; font-weight: bold;">
      <span style="font-family: monospace;"><br>
&nbsp;&nbsp;&nbsp;
&lt;!-- Primary Key --&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &nbsp;
&lt;id name="personId" column="PERSONID"&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &nbsp; &nbsp;
&lt;generator class="increment"/&gt;</span><br
 style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &nbsp;
&lt;/id&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &nbsp; &lt;!--
Direct Data Properties --&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp; &nbsp; &lt;property
name="name"</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
column="NAME"/&gt;</span><br style="font-family: monospace;">
      <span style="font-family: monospace;">&nbsp;&nbsp;&nbsp;
&lt;property name="jobTitle"</span