In all cases,
persistence.xml
always resides in
{root-of-persistence-unit}/META-INF/
directory. For example,
foo.war:
WEB-INF/classes/META-INF/persistence.xml //good
WEB-INF/classes/com/foo123/jpa/Project.class
WEB-INF/web.xml
index.jsp
You may also package entity classes and
persistence.xml
inside a library jar, which is packaged inside the war:
WEB-INF/lib
foo.war:
WEB-INF/lib/my-entities.jar
WEB-INF/web.xml
index.jsp
my-entities.jar:
META-INF/persistence.xml //good
com/foo123/jpa/Project.class
For comparison, some
invalid configurations are:
foo.war:
WEB-INF/persistence.xml //invalid
WEB-INF/web.xml
WEB-INF/classes/com/foo123/jpa/Project.
index.jsp
-----------------------------------------------
foo.war:
META-INF/persistence.xml //invalid
WEB-INF/classes/com/foo123/jpa/Project.class
WEB-INF/web.xml
index.jsp
-----------------------------------------------
foo.war:
persistence.xml //invalid
WEB-INF/classes/com/foo123/jpa/Project.class
WEB-INF/web.xml
index.jsp
-----------------------------------------------
foo.war:
META-INF/persistence.xml //invalid
com/foo123/jpa/Project.class
WEB-INF/web.xml
index.jsp
In all these invalid configurations,
EntityManager
lookup will fail with
javax.naming.NameNotFoundException
, or
@PersistenceContext
injection will be silently skipped and the
em
variable is always
null
, and hence
NullPointerException
when referenced.