Where to put persistence.xml in web app?
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.
22 comments:
Thanks. Simple information that took 1/2 a day to find because it wasn't listed anywhere else.
I wonder if you know how to approach this if you need multiple 'my-entities.jar' files? I.e. if you have multiple persistence units, each with it's own jar file.
If each entity jar is self-contained, meaning it contains all the entity classes and its own persistence.xml, you can just include all such entity jar files in WEB-INF/lib. So you webapp will have multiple persistence.xml, each defining its own persistence unit.
I've discovered that now.
It turns out that it's even more exciting if you're using Spring for dependency injection (transaction management) and Hibernate (JPA provider/Entity Manager)... you need to have your EntityManagerFactory beans definitions in the same compilation unit as the persistence unit if you want Spring to discover the @Entity classes automagically.
Thanks.. finally placed the persistence.xml file in right place.
Hello,
I am working on project which uses EclipseLink and OSGi on Tomcat server. If I put persistence.xml in EclipseLink bundle (META-INF\persistence.xml) and deploy it in tomcat with bridge.war, it works very fine.
But I want to take out persistence.xml out of bundle jar and put that somewhere in Tomcat environment so that DB can be configured without changing the bundle jar.
My question is: Can I put META-INF\persistence.xml in tomcat environment where EclipseLink bundle is in bridge\WEB-INF\eclipse\plugin? If yes, please suggest me, where should I put it.
Thank you very much in advance.
persistence.xml is part of your application and therefore it's best to include it inside your app. It's easier to maintain and understand your app as a self-contained package. If you don't want to change the datasource name in persistence.xml, you can just modify its definition attributes in Tomcat.
Technically, I don't see a way to achieve that. The provider will need a way to relate your persistence.xml to all Entities, and discover all Entities that should be managed. JPA allows an app to enumerate all Entity classes in persistence.xml explicitly. But that's even more trouble than what you are trying to do.
Thank you so much for the information.
It took me ages to find the correct setup: when I imported a Netbeans project into my Eclipse workspace, suddenly I had to tell Eclipse where to put the file on deployment... your post saved the (second) day.
Thanks !
is it possible to put the persistence.xml file in an aar file and load it from there?
Currently I have to copy and rename the aar to jar and put it under tomcat's WEB-INF\lin folder.
Thanks
If you meant EAR file, yes. You can have EAR/lib/foo.jar, which contains META-INF/persistence.xml. This persistence unit will be accessible to the whole EAR.
Thanks for your help at a critical time
i want to develop servlet to jpa communication then, that time where we should put META-INF folder?
good info
lai bhari info
Anyone knows how to make ANT put persistence.xml inside
WAR/WEB-INF/classes/META-INF folder ?
Looks like it's not a standar location.
Thanks.
Ant war task has a nested classes sub-element, which taks a fileset. All files included in this fileset will end up in the WEB-INF/classes directory of the war file.
My GF 3.1.2 keeps on complaining :
May 29, 2013 1:46:01 PM org.glassfish.deployment.admin.DeployCommand execute
SEVERE: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [com..fts.dao.ServiceProviderDao/em] in the scope of the module called [MyApp]. Please verify your application.
May 29, 2013 1:46:01 PM com.sun.enterprise.resource.deployer.JdbcResourceDeployer checkAndDeletePool
WARNING:
May 29, 2013 1:46:01 PM com.sun.enterprise.v3.admin.StopServer doExecute
INFO: Server shutdown initiated
Me-MacBook-Pro:bin me$ find ../domains -name persistence.xml
../domains/domain1/eclipseApps/MyApp/WEB-INF/classes/META-INF/persistence.xml
Unit tests were working OK ...
:(
Any idea ?
Your directory structure looks a bit unusual: domain1/eclipseApps/MyApp
Usually I see user apps appear under the build-in directory domain1/applications/, but you have eclipseApps there instead of the standard applications/
How did you deploy your app?
Thanks, very well explained, solved my problem!
Great and Useful Article.
Online Java Course
Java Online Training
Java Course Online
J2EE training
online J2EE training
Best Recommended books for Spring framework
Java Interview Questions
Java Training Institutes in Chennai
Java Training in Chennai
J2EE Training in Chennai
java j2ee training institutes in chennai
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Internet Of Things training in electronic city
Post a Comment