<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
JBoss : persistence and EntityManager injection
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/zecas">zecas zecas</a> in <i>Beginner's Corner</i> - <a href="http://community.jboss.org/message/566354#566354">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hi,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I'm starting a small testing project from scratch, but I'm getting some trouble configuring JPA persistence ... I'll post some code fragments and hope someone can enlight me on the issue.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I have an EAR, my-ear, and inside there is a single WAR, my-war. All definitions should be placed correctly, since I can make it to pick all settings, even those defined in "persistence.xml".</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="text-decoration: underline;"> my-war structure</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">C:.
|   index.jsp
|
+---META-INF
|       MANIFEST.MF
|
\---WEB-INF
    |   faces-config.xml
    |   jboss-web.xml
    |   web.xml
    |
    +---classes
    |   +---com
    |   |   \---my
    |   |       \---package
    |   |               SimpleMessage.class
    |   |
    |   \---META-INF
    |           persistence.xml
    |
    \---lib
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="text-decoration: underline;"> web.xml</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><web-app ...></span>
    ...
    <span class="jive-xml-tag"><resource-ref></span>
        <span class="jive-xml-tag"><description></span>Datasource Connection<span class="jive-xml-tag"></description></span>
        <span class="jive-xml-tag"><res-ref-name></span>jdbc/MyDB<span class="jive-xml-tag"></res-ref-name></span>
        <span class="jive-xml-tag"><res-type></span>javax.sql.DataSource<span class="jive-xml-tag"></res-type></span>
        <span class="jive-xml-tag"><res-auth></span>Container<span class="jive-xml-tag"></res-auth></span>
        <span class="jive-xml-tag"><res-sharing-scope></span>Shareable<span class="jive-xml-tag"></res-sharing-scope></span>
    <span class="jive-xml-tag"></resource-ref></span>
<span class="jive-xml-tag"></web-app></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="text-decoration: underline;"> jboss-web.xml</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><jboss-web></span>
    <span class="jive-xml-tag"><resource-ref></span>
        <span class="jive-xml-tag"><res-ref-name></span>jdbc/MyDB<span class="jive-xml-tag"></res-ref-name></span>
        <span class="jive-xml-tag"><jndi-name></span>java:/ds/MyDB<span class="jive-xml-tag"></jndi-name></span>
    <span class="jive-xml-tag"></resource-ref></span>
<span class="jive-xml-tag"></jboss-web></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="text-decoration: underline;"> persistence.xml</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag"><span><persistence version="2.0"
    xmlns="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/persistence" target="_blank">http://java.sun.com/xml/ns/persistence</a><span>"
    xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>"></span></span>
    <span class="jive-xml-tag"><persistence-unit name="punit"></span>
        <span class="jive-xml-tag"><jta-data-source></span>java:jdbc/MyDB<span class="jive-xml-tag"></jta-data-source></span>
    <span class="jive-xml-tag"></persistence-unit></span>
<span class="jive-xml-tag"></persistence></span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>My Java code has the following class:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="text-decoration: underline;">SimpleMessage.java</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> com.my.package;
 
<font color="navy"><b>import</b></font> javax.persistence.PersistenceContext;
 
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> SimpleMessage <font color="navy">{</font>
 
    @PersistenceContext
    <font color="navy"><b>private</b></font> EntityManager em;
 
    @PersistenceUnit(name=<font color="red">"punit"</font>)
    <font color="navy"><b>private</b></font> EntityManagerFactory emf;
 
    <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> something() <font color="navy">{</font>
        <font color="navy"><b>if</b></font>( em!=<font color="navy"><b>null</b></font> ) <font color="navy">{</font>
            System.out.println(<font color="red">"EntityManager Available!"</font>);
        <font color="navy">}</font> <font color="navy"><b>else</b></font> <font color="navy">{</font>
            System.out.println(<font color="red">"EntityManager is NULL!"</font>);
        <font color="navy">}</font>
        <font color="navy"><b>if</b></font>( emf!=<font color="navy"><b>null</b></font> ) <font color="navy">{</font>
            System.out.println(<font color="red">"EntityManagerFactory Available!"</font>);
        <font color="navy">}</font> <font color="navy"><b>else</b></font> <font color="navy">{</font>
            System.out.println(<font color="red">"EntityManagerFactory is NULL!"</font>);
        <font color="navy">}</font>
    <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I intend to test just this ... a simple basic class where I inject the manager instance. The class is not an EJB class, just an basic injected class.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>When deploying, I get the following result:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">2010-10-13 14:09:55,197 INFO [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (HDScanner) deploy, ctxPath=/my-war
2010-10-13 14:09:55,213 ERROR [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (HDScanner) ENC setup failed
java.lang.NullPointerException
at org.jboss.injection.PersistenceUnitHandler.getManagedEntityManagerFactory(PersistenceUnitHandler.java:149)
at org.jboss.injection.PcEncInjector.inject(PcEncInjector.java:76)
at org.jboss.web.tomcat.service.TomcatInjectionContainer.populateEnc(TomcatInjectionContainer.java:482)
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>However, if in "persistence.xml" I instead define the datasource reference as "java:ds/MyDB" (datasource name definition on JBoss), I obtain the following result:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">2010-10-13 14:15:20,552 INFO  [org.jboss.jpa.deployment.PersistenceUnitDeployment] (HDScanner)  Starting persistence unit  persistence.unit:unitName=my-ear-1.0.0.ear/my-war-1.0.0.war#punit
2010-10-13 14:15:20,552 INFO [org.hibernate.ejb.Ejb3Configuration] (HDScanner) Processing PersistenceUnitInfo [
name: punit
...]
2010-10-13  14:15:20,552 WARN [org.hibernate.ejb.Ejb3Configuration] (HDScanner)  Persistence provider caller does not implement the EJB3 spec correctly.  PersistenceUnitInfo.getNewTempClassLoader() is null.
2010-10-13  14:15:20,568 INFO  [org.hibernate.cfg.search.HibernateSearchEventListenerRegister]  (HDScanner) Unable to find  org.hibernate.search.event.FullTextIndexEventListener on the classpath.  Hibernate Search is not enabled.
2010-10-13 14:15:20,568 INFO  [org.hibernate.connection.ConnectionProviderFactory] (HDScanner)  Initializing connection provider:  org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
2010-10-13  14:15:20,568 INFO  [org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider]  (HDScanner) Using provided datasource
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) RDBMS: MySQL, version: 5.1.41-3ubuntu12.6
2010-10-13  14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) JDBC  driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.10 (  Revision: ${svn.Revision} )
2010-10-13 14:15:20,568 INFO [org.hibernate.dialect.Dialect] (HDScanner) Using dialect: org.hibernate.dialect.MySQLDialect
2010-10-13  14:15:20,568 INFO [org.hibernate.transaction.TransactionFactoryFactory]  (HDScanner) Transaction strategy:  org.hibernate.ejb.transaction.JoinableCMTTransactionFactory
2010-10-13  14:15:20,568 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] (HDScanner)  instantiating TransactionManagerLookup:  org.hibernate.transaction.JBossTransactionManagerLookup
2010-10-13  14:15:20,568 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] (HDScanner)  instantiated TransactionManagerLookup
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Automatic flush during beforeCompletion(): disabled
2010-10-13  14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner)  Automatic session close at end of transaction: disabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) JDBC batch size: 15
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) JDBC batch updates for versioned data: disabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Scrollable result sets: enabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) JDBC3 getGeneratedKeys(): enabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Connection release mode: auto
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Maximum outer join fetch depth: 2
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Default batch fetch size: 1
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Generate SQL with comments: disabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Order SQL updates by primary key: disabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Order SQL inserts for batching: disabled
2010-10-13  14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Query  translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2010-10-13 14:15:20,568 INFO [org.hibernate.hql.ast.ASTQueryTranslatorFactory] (HDScanner) Using ASTQueryTranslatorFactory
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Query language substitutions: {}
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) JPA-QL strict compliance: enabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Second-level cache: enabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Query cache: disabled
2010-10-13  14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Cache  region factory :  org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
2010-10-13  14:15:20,568 INFO  [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]  (HDScanner) Cache provider: org.hibernate.cache.HashtableCacheProvider
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Optimize cache for minimal puts: disabled
2010-10-13  14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Cache  region prefix:  persistence.unit:unitName=my-ear-1.0.0.ear/my-war-1.0.0.war#punit
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Structured second-level cache entries: disabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Statistics: disabled
2010-10-13  14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner)  Deleted entity synthetic identifier rollback: disabled
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Default entity-mode: pojo
2010-10-13 14:15:20,568 INFO [org.hibernate.cfg.SettingsFactory] (HDScanner) Named query checking : enabled
2010-10-13 14:15:20,568 INFO [org.hibernate.impl.SessionFactoryImpl] (HDScanner) building session factory
2010-10-13  14:15:20,568 INFO [org.hibernate.impl.SessionFactoryObjectFactory]  (HDScanner) Factory name:  persistence.unit:unitName=my-ear-1.0.0.ear/my-war-1.0.0.war#punit
2010-10-13  14:15:20,568 INFO [org.hibernate.util.NamingHelper] (HDScanner) JNDI  InitialContext  properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory,  java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
2010-10-13  14:15:20,584 INFO [org.hibernate.impl.SessionFactoryObjectFactory]  (HDScanner) Bound factory to JNDI name:  persistence.unit:unitName=my-ear-1.0.0.ear/my-war-1.0.0.war#punit
2010-10-13  14:15:20,584 WARN [org.hibernate.impl.SessionFactoryObjectFactory]  (HDScanner) InitialContext did not implement EventContext
2010-10-13  14:15:20,584 INFO [org.hibernate.util.NamingHelper] (HDScanner) JNDI  InitialContext  properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory,  java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
2010-10-13 14:15:20,724 INFO [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (HDScanner) deploy, ctxPath=/my-war
2010-10-13  14:15:20,755 INFO [javax.enterprise.resource.webcontainer.jsf.config]  (HDScanner) Initializing Mojarra (1.2_12-b01-FCS) for context '/my-war' 
2010-10-13 14:15:20,896 ERROR [STDERR] (HDScanner) SLF4J: Class path contains multiple SLF4J bindings.
2010-10-13  14:15:20,896 ERROR [STDERR] (HDScanner) SLF4J: Found binding in  [vfszip:/C:/Program  Files/Java/jboss-5.1.0.GA/common/lib/slf4j-jboss-logging.jar/org/slf4j/impl/StaticLoggerBinder.class]
2010-10-13  14:15:20,896 ERROR [STDERR] (HDScanner) SLF4J: Found binding in  [vfszip:/C:/Program  Files/Java/jboss-5.1.0.GA/server/default/deploy/my-ear-1.0.0.ear/logback-classic-0.9.20.jar/org/slf4j/impl/StaticLoggerBinder.class]
2010-10-13  14:15:20,912 ERROR [STDERR] (HDScanner) SLF4J: See  http://www.slf4j.org/codes.html#multiple_bindings for an explanation.</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>So my questions are:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><strong>1#</strong> After invoking "SimpleMessage.something();" (using the second option "java:ds/MyDB" in "persistence.xml") I receive the following output:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code">EntityManager is NULL!
EntityManagerFactory is NULL!
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>So no injection happened ... shouldn't it occur? what happened?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><br/><strong>2#</strong> I was assuming that "web.xml" defines the JNDI to be used inside my webapp, then "jboss-web.xml" would map that name into the internall JNDI defined in JBoss.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>In this case for JBoss usage, if changing to IBM WebSphere I would use another mapping config file.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>BUT ... I was also assuming that "persistence.xml" would define the JNDI that is defined in "web.xml" ... and not the one defined in application server level ... so I would get an abstraction from whichever server I'll deploy my project into.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Shouldn't this be working like that? What am I doing wrong?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Any help would be appreciated ... I really need to understand and put this to work ...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Thanks.</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/566354#566354">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Beginner's Corner at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>