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 <[email protected]> > An: [email protected] > CC: [email protected] > 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 <[email protected]> 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
