Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes EJB and other Java EE Technologies and the fly likes injecting @PersitenceContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "injecting @PersitenceContext " Watch "injecting @PersitenceContext " New topic

injecting @PersitenceContext

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228

Can we inject @PersitenceContext in non managed objects? Injecting @PersitenceContext works fine in EJB and Servlets but when i try to inject it in a regular java class within the .ear file i am getting null pointer exception. Could some one clarify me about this?

Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10339

The @PersistenceContext is injected by the server. The server only scans those classes whose lifecycle is managed by the server (example: servlet, EJB etc...). So if you are injecting it in a class which is not managed by the server, then the injection will not happen.

[My Blog] [JavaRanch Journal]
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228

I am trying to create entity manager factory in my web application as

I have persistence.xml file in EAR/META-INF

I get exception couldn't find persistence unit. How to create entity manager in a non managed class in web application?

Does my question make any sense?

I agree. Here's the link:
subject: injecting @PersitenceContext