aspose file tools*
The moose likes Object Relational Mapping and the fly likes Injecting PersistenceContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Injecting PersistenceContext" Watch "Injecting PersistenceContext" New topic
Author

Injecting PersistenceContext

satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
Am trying to inject a persistenceContext in Jboss deployed app

However I keep getting a null pointer exception - the PersistenceContext is not being injected .

Am trying to inject the PC in a non ejb object

Would this be the cause ?

Can it only be injected in ejbs ?

Thanks ,
~satish
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9313
    
109

Which version of JBoss?


[My Blog] [JavaRanch Journal]
satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
using Jboss 4.2.2 GA

Thanks ,
~satish
satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
On deploying a session bean ( stateless ) and then injecting a PersistenceContext in the session bean works for me .


So to inject PC is it only possible when using Session beans ?

If yes - than how do I make use of a container managed Entity Manager minus the injection ?

Thanks ,
~satish
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9313
    
109

The @PersistenceContext injection is only supported in managed objects (EJBs, servlets etc...). Where are you injecting this PersistenceContext? In JBoss-4.2.x the injection of PersistenceContext is probably not supported in web tier too (because 4.2.x is a J2EE1.4 server with EJB3 patch).

If yes - than how do I make use of a container managed Entity Manager minus the injection ?


A JNDI lookup will give you the persistence context. See "3.4.2. Referencing from Global JNDI" at EntityManager configuration in JBoss
satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
Thanks Jaikiran for the inputs .

In my sample test that I am doing - I am invoking a jsp from browser

The jsp is creating an instance of a simple Java Class that I call as the "delegate" .
The delegate is being injected with the PC

I know this is not good design where JSP is starting the invocation - but I just wanted to play around with ejb's

Since my " delegate " is not managed - it too cannot make use of DI ?

Here is the sample code used :
This is the JSP


This is the "delegate" :


Thanks Jai for the inputs and help.
~satish
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9313
    
109

Since my " delegate " is not managed - it too cannot make use of DI ?


The application server will not scan this class since its not managed. So yes, you cannot inject it here.
satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
Thanks Jaikiran for helping me out .

Regards,
~satish
 
jQuery in Action, 2nd edition
 
subject: Injecting PersistenceContext
 
Similar Threads
Injecting @Resource UserTransaction results in null
How to obtain entity manager in application server?
How to inject EntityManager via setter in spring
One of more persistence context for separate projects?
How to create a Template for EntityManager in a WebApplication