You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by Kevin Sutter <kw...@gmail.com> on 2010/12/22 16:00:15 UTC

Re: @PersistenceContext not working

Hi sim085,
Even though the @PersistenceUnit and @PersistenceContext annotations are
defined by the JPA specification, the actual processing of these annotations
are performed by the application server.  These annotations signal to the
application server that some processing needs to be done between the app
server (container) and the jpa provider -- like the creation of an EMF
and/or EM.  And, then these instances get injected into the bytecodes of the
Stateless Session Bean and/or Servlet.

Thus, I am going to cross link this message over to the Geronimo Users forum
to see if they can help you out.

Thanks,
Kevin

On Wed, Dec 22, 2010 at 8:28 AM, sim085 <si...@hotmail.com> wrote:

>
> Hello to all, I am having problems with @PersistenceContext. It seems that
> this is not initializing the EntityManager and therefore this is remaining
> null. My code is as follows;
>
> -----
> @PersistenceContext(unitName="TestApp")
> private EntityManager entityManager;
> …
> public User findUser(Long id){
>        User user = this.entityManager.find(User.class, id);
>        return user;
> }
> -----
>
>
> This code is in a Stateless EJB which is deployed as part of an EAR
> application. The EAR application also has a WAR file which makes use of the
> above code. [NOTE: I am not using the @PersistenceContext from a Servlet. I
> am using it from Stateless EJB. The Servlet calls this Stateless EJB. I am
> saying this because I read that @PersistenceContext should not be used from
> Servlets.]
>
> The problem is that after deployment I get a NullPointerException saying
> that entityManager has not been initialized. However if I change the code
> as
> follows everything works fine.
>
> -----
> @PersistenceContext(unitName="TestApp")
> private EntityManager entityManager;
> …
> public User findUser(Long id) {
>        if(this.entityManager == null){
>                EntityManagerFactory factory =
>                        Persistence.createEntityManagerFactory(
>                                "TestApp", System.getProperties());
>                this.entityManager = factory.createEntityManager();
>                System.out.println("EntityManager initialized using
> EntityManagerFactory");
>        }
>        User user = this.entityManager.find(User.class, id);
>        return user;
> }
> -----
>
>
> As can be seen from the above code I am initializing the
> EntityManagerFactory using Persistence.createEntityManagerFactory. I also
> tried initializing the EntityManagerFactory using @PersistenceUnit
> annotation but this does not work either. In other words it seems that JPA
> annotations are not working.
> I did a search over the Internet and I read that the problem could be that
> the annotations used as sub-modules of other modules (ex: Under the
> WEB-INF/lib folder of the WAR file) do not get their annotations processed.
> However I tried to counter this by putting my EJB module in the EAR file
> and
> deleteing the module from the WAR file which is also in the EAR file. The
> deployment works fine. However with the second code I still get the message
> “EntityManager initialized using EntityManagerFactory” which means that the
> @PersistenceContext was not processed.
> Does anyone know what I might be doing wrong!?
> My persistence.xml file is as follows. I am using RESOURCE_LOCAL (which
> mind
> you works on Geronimo but not on GlassFish) because of other problems
> related with using data sources.
>
> -----
>    <persistence-unit name="TestApp" transaction-type="RESOURCE_LOCAL">
>
>  <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
>                <!--
>                        <jta-data-source>MyDatasource</jta-data-source>
>
>  <non-jta-data-source>MyNonDatasource</non-jta-data-source>
>                -->
>                <properties>
>                        <property name="openjpa.jdbc.SynchronizeMappings"
> value="buildSchema(ForeignKeys=true)"/>
>                        <property name="openjpa.ConnectionURL"
> value="jdbc:derby://localhost:1527/MyDatabase"/>
>                        <property name="openjpa.ConnectionDriverName"
> value="org.apache.derby.jdbc.ClientDriver"/>
>                        <property name="openjpa.ConnectionUserName"
> value="admin"/>
>                        <property name="openjpa.ConnectionPassword"
> value="password"/>
>                        <property name="openjpa.Log"
> value="DefaultLevel=WARN, Tool=INFO"/>
>                        </properties>
>        </persistence-unit>
> </persistence>
> -----
>
> I do not know if this is a problem with Geronimo or OpenJPA. The reason I
> posted here is because I am using OpenJPA as my Provider and therefore I am
> assuming that OpenJPA is responsable to process the @PersistenceContext
> annotion. I am not sure of this. Sorry if I posted this in the wrong place.
> The @Stateless annotions however are being processed correctly.
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/PersistenceContext-not-working-tp5859331p5859331.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: @PersistenceContext not working

Posted by sim085 <si...@hotmail.com>.
Sorry forgot to mention that I did create the MyNoJtaDS and the MyJtaDS. To
create these datasources I went to "Database Pools" and used the "Geronimo
database pool wizard".
-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2134398.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by KHAksnes <kh...@gmail.com>.
The eclipse plugin is another problem, there are problems with all versions
as far as I know which might or might not bite you depending on which
versions of GEP and Geronimo you are using; See this thread for more
information:
http://apache-geronimo.328035.n3.nabble.com/GEP-version-blues-td2091121.html
-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2142296.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by Julian Klappenbach <jk...@gmail.com>.
One of the problems I've been seeing is that, while "usage" provides a few
examples of how to draft a "plan", it misses one important use case of
drafting an "openejb-jar.xml" file.  Having that would really, really help.

--
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p3958012.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by sim085 <si...@hotmail.com>.
Are there any good books about Geronimo which I can buy to help me achieve
this? I have no idea what additional files I have missing and what I need to
put in these files and why. After two weeks I still did not manage to
install this simply ejb jar file. Therefore I think a good book would really
help me out. 
-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2171907.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by sim085 <si...@hotmail.com>.

> Open  the description and read it;

Thanks. I re-read the description and copied the geronimo-web.xml into my
openejb-jar.xml. I read the exceptions I was getting on deployment very
carefully and removed the parts it was complaining about. Now I finally
managed to deploy my ejb jar file on it's own and also as part of an ear
file without any problems. HOWEVER I still have problems :( 

Here is my openejb-jar.xml

-----
<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2">
    
	<environment>
        <moduleId>
            <artifactId>MyJPA</artifactId>
        </moduleId>
        <dependencies>
            <dependency>
                <groupId>console.dbpool</groupId>
                <artifactId>My_JPA_JTA_DS</artifactId>
            </dependency>
        </dependencies>
    </environment>

</openejb-jar> 
-----

My problem is that I still get a java.lang.NullPointerException wehn I try
to use the EntityManager initialised using the @PersistenceContext. My
persistence.xml is now as follows;

-----
<?xml version="1.0" encoding="UTF-8"?>
<persistence ... >
	<persistence-unit name="TestApp" transaction-type="JTA">
	
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<jta-data-source>My_JPA_JTA_DS</jta-data-source>
	</persistence-unit>
</persistence>
-----

and the code is as follows;

-----
	@PersistenceContext(unitName="TestApp")
	private EntityManager entityManager;
-----

I really don't know what I am doing wrong and why @PersistenceContext is not
initialising my EntityManage. As much as I know all should be fine and
therefore do not know what I am lacking!
-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2148211.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by KHAksnes <kh...@gmail.com>.
If you have created DB Pools their name is likely to be grouped under
console.dbpool.

In geronimo console please select the DBPool entry. For each existing dbpool 
you will find 3 links, one to edit the dbpool description, one to delete it
and one describing how to use it. Open  the description and read it;  It
contains working examples ready to paste into your own config files. :-)
-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2142274.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by sim085 <si...@hotmail.com>.

> Did you remember to add dependencies .. to openejb-jar.xml

Hello, thanks for your reply. After reading your post I created an
openejb-jar.xml file. I tried looking on the net to see how exactly to do
this, but being my first time I really found it hard to understand what I
need to include or not. I also tried to download the Eclipse plugin but this
did not work.

Finally I have managed to arrive to this.

-----
<?xml version="1.0" encoding="UTF-8"?>

<openejb-jar xmlns="http://openejb.apache.org/xml/ns/openejb-jar-2.2">
    <enterprise-beans>
        <session>
	            <ejb-name>UserEntityManagerBean</ejb-name>
		    <resource-ref>
		        <ref-name>jdbc/My_JPA_JTA_DS</ref-name>
		        <resource-link>My_JPA_JTA_DS</resource-link>
		    </resource-ref>
		</session>
    </enterprise-beans>
</openejb-jar>
-----

When I deploy my ejb module (jar file that is) with the above
openejb-jar.xml descriptor I get no error regarding the file itself. However
I am still getting the same error as before. 

-----
he application was not deployed.
Unable to resolve reference "JtaDataSourceWrapper"
    in gbean
default/myjpa-1.0-SNAPSHOT.jar/1293197470229/car?EJBModule=default/myjpa-1.0-SNAPSHOT.jar/1293197470229/car,J2EEApplication=null,PersistenceUnitModule=ejb.jar,j2eeType=PersistenceUnit,name=My_JPA_PU
    to a gbean matching the pattern
[?name=jdbc/My_JPA_JTA_DS#org.apache.geronimo.naming.ResourceSource]
    due to: No matches for referencePatterns:
[?name=jdbc/My_JPA_JTA_DS#org.apache.geronimo.naming.ResourceSource]
-----


The problem I am having is obviously related with my lack of knowledge on
Geronimo, OpenEJB and the files that these two framework require. As I said
I am following a book to learn this technology and no where was there
written about the need for these additional files. 

Does anyone know about any documentation that shows how I can achieve the
above in terms of Geronimo and OpenEJB? 

How do you people build your geronimo-application.xml and openejb-jar.xml?

-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2141115.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by KHAksnes <kh...@gmail.com>.
Did you remember to add dependencies to the JTA and the NonJTA dbpools to
openejb-jar.xml or geronimo-application.xml?
Click usage on the DBPool in question to get a description on how to use it. 
-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2138187.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by sim085 <si...@hotmail.com>.

> @PersistenceContext is for container-managed entity 
> managers only.

The only reason I used transaction-type="RESOURCE_LOCAL" is because 
transaction-type="JTA" does not seem to work when I deploy the EAR
application. As much as I know when  transaction-type="JTA" then you need to
define the jta-data-source and the non-jta-data-source. However when I try
to deploy the EAR file I get the following error;

-----
The application was not deployed.
Unable to resolve reference "NonJtaDataSourceWrapper"
    in gbean
default/MyEAR/1293337922310/car?EJBModule=MyJPA.jar,J2EEApplication=default/MyEAR/1293337922310/car,PersistenceUnitModule=MyJPA.jar,j2eeType=PersistenceUnit,name=TestPU
    to a gbean matching the pattern
[?name=MyNoJtaDS#org.apache.geronimo.naming.ResourceSource]
    due to: No matches for referencePatterns:
[?name=MyNoJtaDS#org.apache.geronimo.naming.ResourceSource]
Unable to resolve reference "JtaDataSourceWrapper"
    in gbean
default/MyEAR/1293337922310/car?EJBModule=MyJPA.jar,J2EEApplication=default/MyEAR/1293337922310/car,PersistenceUnitModule=MyJPA.jar,j2eeType=PersistenceUnit,name=TestPU
    to a gbean matching the pattern
[?name=MyJtaDS#org.apache.geronimo.naming.ResourceSource]
    due to: No matches for referencePatterns:
[?name=MyJtaDS#org.apache.geronimo.naming.ResourceSource]
org.apache.geronimo.common.DeploymentException: Unable to resolve reference
"NonJtaDataSourceWrapper"
    in gbean
default/MyEAR/1293337922310/car?EJBModule=MyJPA.jar,J2EEApplication=default/MyEAR/1293337922310/car,PersistenceUnitModule=MyJPA.jar,j2eeType=PersistenceUnit,name=TestPU
    to a gbean matching the pattern
[?name=MyNoJtaDS#org.apache.geronimo.naming.ResourceSource]
    due to: No matches for referencePatterns:
[?name=MyNoJtaDS#org.apache.geronimo.naming.ResourceSource]
Unable to resolve reference "JtaDataSourceWrapper"
    in gbean
default/MyEAR/1293337922310/car?EJBModule=MyJPA.jar,J2EEApplication=default/MyEAR/1293337922310/car,PersistenceUnitModule=MyJPA.jar,j2eeType=PersistenceUnit,name=TestPU
    to a gbean matching the pattern
[?name=MyJtaDS#org.apache.geronimo.naming.ResourceSource]
    due to: No matches for referencePatterns:
[?name=MyJtaDS#org.apache.geronimo.naming.ResourceSource]
-----

I had already made a post about this on the OpenJPA forum and this was
re-posted here;
http://apache-geronimo.328035.n3.nabble.com/Re-Problems-when-setting-jta-data-source-value-td2128499.html
-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2134389.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by KHAksnes <kh...@gmail.com>.
>From chapter 5 in the EJB 3.0 persistence spec (top of page 118):
An entity manager whose underlying transactions are controlled through JTA
is termed a JTA entity
manager.
An entity manager whose underlying transactions are controlled by the
application through the EntityTransaction
API is termed a resource-local entity manager.
A container-managed entity manager must be a JTA entity manager. JTA entity
managers are only specified
for use in Java EE containers.
An application-managed entity manager may be either a JTA entity manager or
a resource-local entity
manager.

In other words a resource-local entity mangager is always
application-managed. You will probably want to use something like:

@PersistenceUnit("TestApp") 
EntityManagerFactory emf;

to get the EntityManagerFactory.
@PersistenceContext is for container-managed entity managers only. 


-- 
View this message in context: http://apache-geronimo.328035.n3.nabble.com/Re-PersistenceContext-not-working-tp2131727p2133973.html
Sent from the Users mailing list archive at Nabble.com.

Re: @PersistenceContext not working

Posted by Stefan Schilling <ma...@gmx.de>.
Hello Sim,

question: how do you instantiate the class holding the EntityManager object? Do you do a simple ClassX x = new ClassX(); call (assuming that ClassX contains the following code:

@PersistenceContext(unitName="TestApp")
private EntityManager entityManager; 

)? Or do you have the container to lookup ClassX? Remember, you *have* to have the container to somehow lookup the ClassX (e.g. via @EJB annotation or a Context.lookup call).

Bye,
Stefan

-------- Original-Nachricht --------
> Datum: Wed, 22 Dec 2010 09:00:15 -0600
> Von: Kevin Sutter <kw...@gmail.com>
> An: users@openjpa.apache.org
> CC: user@geronimo.apache.org
> Betreff: Re: @PersistenceContext not working

> Hi sim085,
> Even though the @PersistenceUnit and @PersistenceContext annotations are
> defined by the JPA specification, the actual processing of these
> annotations
> are performed by the application server.  These annotations signal to the
> application server that some processing needs to be done between the app
> server (container) and the jpa provider -- like the creation of an EMF
> and/or EM.  And, then these instances get injected into the bytecodes of
> the
> Stateless Session Bean and/or Servlet.
> 
> Thus, I am going to cross link this message over to the Geronimo Users
> forum
> to see if they can help you out.
> 
> Thanks,
> Kevin
> 
> On Wed, Dec 22, 2010 at 8:28 AM, sim085 <si...@hotmail.com> wrote:
> 
> >
> > Hello to all, I am having problems with @PersistenceContext. It seems
> that
> > this is not initializing the EntityManager and therefore this is
> remaining
> > null. My code is as follows;
> >
> > -----
> > @PersistenceContext(unitName="TestApp")
> > private EntityManager entityManager;
> > …
> > public User findUser(Long id){
> >        User user = this.entityManager.find(User.class, id);
> >        return user;
> > }
> > -----
> >
> >
> > This code is in a Stateless EJB which is deployed as part of an EAR
> > application. The EAR application also has a WAR file which makes use of
> the
> > above code. [NOTE: I am not using the @PersistenceContext from a
> Servlet. I
> > am using it from Stateless EJB. The Servlet calls this Stateless EJB. I
> am
> > saying this because I read that @PersistenceContext should not be used
> from
> > Servlets.]
> >
> > The problem is that after deployment I get a NullPointerException saying
> > that entityManager has not been initialized. However if I change the
> code
> > as
> > follows everything works fine.
> >
> > -----
> > @PersistenceContext(unitName="TestApp")
> > private EntityManager entityManager;
> > …
> > public User findUser(Long id) {
> >        if(this.entityManager == null){
> >                EntityManagerFactory factory =
> >                        Persistence.createEntityManagerFactory(
> >                                "TestApp", System.getProperties());
> >                this.entityManager = factory.createEntityManager();
> >                System.out.println("EntityManager initialized using
> > EntityManagerFactory");
> >        }
> >        User user = this.entityManager.find(User.class, id);
> >        return user;
> > }
> > -----
> >
> >
> > As can be seen from the above code I am initializing the
> > EntityManagerFactory using Persistence.createEntityManagerFactory. I
> also
> > tried initializing the EntityManagerFactory using @PersistenceUnit
> > annotation but this does not work either. In other words it seems that
> JPA
> > annotations are not working.
> > I did a search over the Internet and I read that the problem could be
> that
> > the annotations used as sub-modules of other modules (ex: Under the
> > WEB-INF/lib folder of the WAR file) do not get their annotations
> processed.
> > However I tried to counter this by putting my EJB module in the EAR file
> > and
> > deleteing the module from the WAR file which is also in the EAR file.
> The
> > deployment works fine. However with the second code I still get the
> message
> > “EntityManager initialized using EntityManagerFactory” which means
> that the
> > @PersistenceContext was not processed.
> > Does anyone know what I might be doing wrong!?
> > My persistence.xml file is as follows. I am using RESOURCE_LOCAL (which
> > mind
> > you works on Geronimo but not on GlassFish) because of other problems
> > related with using data sources.
> >
> > -----
> >    <persistence-unit name="TestApp" transaction-type="RESOURCE_LOCAL">
> >
> > 
> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
> >                <!--
> >                        <jta-data-source>MyDatasource</jta-data-source>
> >
> >  <non-jta-data-source>MyNonDatasource</non-jta-data-source>
> >                -->
> >                <properties>
> >                        <property name="openjpa.jdbc.SynchronizeMappings"
> > value="buildSchema(ForeignKeys=true)"/>
> >                        <property name="openjpa.ConnectionURL"
> > value="jdbc:derby://localhost:1527/MyDatabase"/>
> >                        <property name="openjpa.ConnectionDriverName"
> > value="org.apache.derby.jdbc.ClientDriver"/>
> >                        <property name="openjpa.ConnectionUserName"
> > value="admin"/>
> >                        <property name="openjpa.ConnectionPassword"
> > value="password"/>
> >                        <property name="openjpa.Log"
> > value="DefaultLevel=WARN, Tool=INFO"/>
> >                        </properties>
> >        </persistence-unit>
> > </persistence>
> > -----
> >
> > I do not know if this is a problem with Geronimo or OpenJPA. The reason
> I
> > posted here is because I am using OpenJPA as my Provider and therefore I
> am
> > assuming that OpenJPA is responsable to process the @PersistenceContext
> > annotion. I am not sure of this. Sorry if I posted this in the wrong
> place.
> > The @Stateless annotions however are being processed correctly.
> >
> >
> > --
> > View this message in context:
> >
> http://openjpa.208410.n2.nabble.com/PersistenceContext-not-working-tp5859331p5859331.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >

-- 
GMX DSL Doppel-Flat ab 19,99 Euro/mtl.! Jetzt mit 
gratis Handy-Flat! http://portal.gmx.net/de/go/dsl