what are the possible locations of persistence.xml file in a web archive in jpa and how to specify that location for getting EntityManagerFactory? - jpa

**EntityManagerFactory factory=Persistence.getEnityManagerFactory(???);**
In specification they are explained in the context of EJBs.But i am not understanding the actual possible locations of persistence.xml in a web archive.

The persistence.xml must always be in the META-INF directory on the classpath. Normally it will be inside a jar that is on the classpath, such as a jar in /WEB-INF/lib/ in your war file.

Related

Persistence.xml in WAR and JAR

I have a jar file that cares about all persistence logic. This jar file contains persistence.xml file with all configurations needed. My war uses this jar file as dependency but should not know how it's persisted in database. I have some questions:
1) My WAR have #Entity classes; Do I need to create a persistence.xml in "resources/META-INF"? I already have this in the JAR file.
2) If I need to create another persistence.xml in the WAR, how can I inherite this persistence.xml from the jar-file?

JBOSS7 external jar with dependency on ear file

I am using JBOSS 7
In my custom jar I am implementing an Interface. The interface is in a jar packaged with ECM.ear file. Unless I put my custom jar inside the web-inf/lib folder (of the war file located in ear file) I am getting ClassNotFoundException w.r.to the interface.
I created a module for my custom jar but I don't how to set up a dependency with ear file. I copied the jar containing the interface say mdm.jar and placed it in the module and also added an entry in the resource root of module.xml. After restarting I am getting ClassNotFoundException for the classes referred by mdm.jar, which arein ear file.
How to achieve this dependency?
Thanks,
Raghu
JBOSS 7 needs you to place the packaged jar files in the lib folders of your web-inf/lib or the ear/lib cos of the Class Loading Precedence that JBOSS server follows.
Alternatively you could load it as a module, but you need to specify any addition of this kind outside of JBOSS default supplied modules using your MANIFEST file or jboss-deployment-structure.xml
This link should provide you more insight on what would suit you best.
Hope it helps.

No persistence.xml file found in project

I'm using JBoss Developer Studio 7 (alpha) and have an EAR project with two WARs and one JAR. The JAR is a JPA project. The JAR contains all JPA Entities and also persistance.xml (inside META-INF folder). So I'd like to use the persitence unit defined in the JAR from both WARs. Since the idea is that each WAR has to use the shared persistance.xml from the JAR (JPA project) y removed the persitence.xml in each WAR MATA-INF folder, but then when I want to build the WAR I get "No persistence.xml file found in project (WAR name)".
How I resolve this? If I set some dummy configuration in the WAR persistance.xml the IDE let me buid withot errors, but I don't like this, I have two persitence units that never will be used.

How to store persistence.xml with other configuration files

I'm using EclipseLink as my JPA implementation. Our project has a directory where we keep all of our config files, and I would like to store my persistence.xml file in the config directory, but cannot find any way to tell createEntityManagerFactory to look there to find the persistence-unit.
It doesn't answer your question directly, but if you want to do it in order to externalize configuration of the application, I think the best approach would be to extract all configurable properties (such as connection settings, etc) into a separate properties file and pass them to createEntityManagerFactory() as a Map (also note that you can override properties from persistence.xml this way).
Then your persistence.xml will contain only non-configurable settings (such as a list of persistent classes, etc), i. e. it won't make sense to change these properties without rebuilding the whole application, and you can leave it in its default location.
You can achieve this by using spring-orm LocalContainerEntityManagerFactory (you don't need to use all the spring context stuff if you don't want to).
LocalContainerEntityManagerFactory lcemf = new LocalContainerEntityManagerFactory ();
lcemf.setPersistenceUnitName("some_pu");
lcemf.setpersistenceXmlLocation("file:/data/config.xml");
EntityManagerFactory emf = lcemf.createNativeEntityManagerFactory();
you can also ignore the xml once and for all, and configure all in code by using the LocalContainerEntityManagerFactory (see setPackagesToScan).
persistence.xml must always be present within META-INF directory in the classpath. More on where should META-INF be present for different types of java applications can be found here.
Persistence units are defined by the persistence.xml configuration file. The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit’s root.
Each persistence unit must be identified with a name that is unique to the persistence unit’s scope.
Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR file that can then be included in an WAR or EAR file.
If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should be put in the EJB JAR’s META-INF directory.
If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.

Where should beans.xml be placed?

I've just upgraded to NetBeans 7.1 from 7.0. On opening a JSF managed bean it "helpfully" told me that it couldn't find my beans.xml file so would I like it created for me. I knew I had a beans.xml file under WEB-INF but I said yes anyway to see what happened. A new beans.xml was created under META-INF for me.
Thinking I'd made a mistake I deleted the file under WEB-INF only to have my application fail at start up. Putting beans.xml back into WEB-INF fixed that problem. This page seems to think both locations are valid: http://seamframework.org/Documentation/WhatIsBeansxmlAndWhyDoINeedIt
So, the question is which folder should beans.xml live in WEB-INF or META-INF?
I'm running GlassFish 3.1.1 and Java 7
Having beans.xml placed to the WEB-INF directory is fine in your case, because most likely beans are under WEB-INF classes.
Correct place for beans.xml depends about type of archive and location of bean classes.
In specification this is explained as follows:
Bean classes of enabled beans must be deployed in bean archives.
A library jar, EJB jar, application client jar or rar archive is a bean archive if it has a file named beans.xml in the META-INF directory.
The WEB-INF/classes directory of a war is a bean archive if there is a file named beans.xml in the WEB-INF directory of the war.
A directory in the JVM classpath is a bean archive if it has a file named beans.xml in the META-INF directory.