I'm confused about the @PersistenceContext annotation.
If you look at its documentation, it says, about its "name" attribute:
"The name by which the entity manager is to be accessed in the environment referencing context, and is not needed when dependency injection is used."
OK. So that means that within some SLSB somewhere I can do this:
[code]@PersistenceContext
private EntityManager entityManager;[/code]
...instead of this:
[code]@PersistenceContext(unitName="froobyFrobby")
private EntityManager entityManager;[/code]
...and then somewhere in my code I can say this:
[code]assert this.entityManager != null;[/code]
...and [i]some[/i] persistence unit's EntityManager will have been injected in here. (Which one? The specification doesn't say, as far as I can tell.)
So, OK, I throw this EJB into an .ear file and attempt to deploy it with the verifier on. The verifier yells at me because I've neglected to put a persistence.xml in my .ear's META-INF directory. My bad. So I do this...and it looks like:
[code]<persistence>
<persistence-unit name="someNameHere"/>
</persistence>[/code]
Fine so far. (Note that my persistence root is now the .ear file. Note as well that the name I've picked for my one persistence unit doesn't appear anywhere else. I think that's legal.)
Now I run the verifier, and among the steaming piles of log information it deposits on my hard drive is the following:
[code]--------------
FAILED TESTS :
--------------
Test Name : tests.ejb.ejb30.PUMatchingEMandEMFRefTest
Test Assertion : For every entity manager referenced in the application,
there must be a matching persistence unit defined using META-INF/persistence.xml
file. Please refer to EJB 3.0 Persistence API Specification section #6.2 for further information.
Test Description : For [ sapling-ear-1.0-SNAPSHOT#bricks-business-query-ejb3-1.0-SNAPSHOT.jar#QueryLogicDelegate ]
There is no unique persistence unit found by name [ null ] in the scope of this component.
Persistence units that are visible to this component are [ ].[/code]
O...K. So I consult section #6.2...and I see nothing that looks to me like a justification for this test. Is there some line in 6.2 somewhere that will tell me both:
(a) why it is that I don't have to supply a "unitName" attribute to my @PersistenceContext annotation, even though
(b) it is required that this unnamed @PersistenceContext somehow "link up" with a named persistence unit in my persistence.xml file?
The other thing that bothers me a bit here is that the EJB jar that failed the test can't "see" any persistence units, even though there is one defined in its containing ear's META-INF/persistence.xml file.
So, then, three questions:
1. What persistence unit is supposed to be selected when one uses a simple @PersistenceContext annotation (i.e. one does not supply a unitName attribute)?
2. What is the verifier trying to do, and where did it get its rules in this department?
3. How does one make an .ear-scoped persistence.xml file visible to the components in the .ear, if not by putting it in $EAR_ROOT/META-INF?
The reason I'm loath to put a persistence context with a unit name in here is that I don't want to specify it at the "library" level. That is, I have a (hopefully) reusable SLSB in a library that, by definition, doesn't know what application it's going to be part of. So I annotate its entityManager field with a raw @PersistenceContext annotation.
I hope this all makes sense; thanks for reading.
Best,
Laird
[Message sent by forum member 'ljnelson' (ljnelson)]
http://forums.java.net/jive/thread.jspa?messageID=262548