File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes @PersistenceContext - overriding injection using configuation file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "@PersistenceContext - overriding injection using configuation file." Watch "@PersistenceContext - overriding injection using configuation file." New topic

@PersistenceContext - overriding injection using configuation file.

Tomasz Wolak

Joined: May 06, 2008
Posts: 10
I thought that you should be able to override @PersistenceContext annotation using ejb-jar.xml file. Either I am doing something wrong or there is another issue that I am not aware of.
Simple concept: to learn innards of EJB I created a very simple eCommerce app. The store has two diffrent sources of inventory (let say we can sell shoes and shirts). To manage the inventory I created an inventory manager. You need separate manager to manage each inventory source. Since the managers perform the same functions and only differ in which inventory source they manipulate I only need one class and I'll configure two different instances. Here is my setup:
Code for the Manager

Code for JSF bean that calls the manager:


I am deploying on Jboss5.0 and the server comes up clean, there are two beans registered, I have two different datastores configured and accessible. However, when I try to run it I always end up creating the new stock in ONE database ("InvShoes") so the one that was injected using @PersistenceContext annotation. I can fix this really easy if I remove the @PersistenceContext(unitName = "InvShoes") annotation from the bean. Therefore, it appears that @PersistenceContext is not being overwritten by the values in the ejb-jar.xml. It was my understanding that the configuration file always trumps the annotation so what is going on here?
Appreciate your input.
Akshay Sahu

Joined: Sep 04, 2009
Posts: 26

Hi Tomasz Wolak,

In EJB 3.0, except for the enterprise bean type value, every annotation enabled configuration is overriden by ejb-jar.xml file configurations.

An enterprise bean annotated stateless cannot be overriden as Stateful at ejb-jar.xml file.

Your query, code and question looks ambigious to me.

I request you to be more clear in your question so that I can help you out ... !!!

Akshay Sahu
OCBCD (EJB 3), OCJP (Java SE 6), MCTS (MS SQL Server 2005)
Tomasz Wolak

Joined: May 06, 2008
Posts: 10
The concept is very simple: one class definition, two references each defined in ejb-jar.xml. Even though references have been configured to use different persistence context (InvShoes/InvShirts) they both use the persistence context that was injected using the annotation (InvShoes). The only way I get the reference to use the configured persistence context is to remove the annotation that injects the persistence context.
Given the server I use (Jboss 5) jdk 1.6 and ebj-jar.xml definition why is configuration NOT overriding the injection via annotation.
Not sure how much simpler I can make this.
I agree. Here's the link:
subject: @PersistenceContext - overriding injection using configuation file.
It's not a secret anymore!