I have one jar library A (or project in eclipse), which has it's own persistence unit (META-INF/persistence.xml) and some entity classes, and another project (B) using this one. In project B there is also persistence unit and entity classes.
In project B I need to use both entity classes from project A and B. But if I set "A" as persistence unit name, EntityManager cannot create named query if this query is in entity from project B. If I set "B" as persistence unit name, it cannot create named queries from entities from project A. Error message is:
NamedQuery of name: MyEntityName.myQueryName not found.
Can persistence units somehow include other persistence units? Or is there any other way to solve this problem?
EclipseLink 2.3 introduced Composite Persistence Units, which allows you to create a persistence unit that essentially acts only as a container for two or more actual persistence units. You are then able to use this single composite persistence unit in your application as if you had only one persistence unit. This should meet your goals of keeping your persistence.xml files clean for easy synchronization of your model to database. Pretty cool stuff.
You can list your classes needed by A in one persistence unit, and classes needed you B in an other:
<persistence ...>
<persistence-unit name="projectA" ...>
....
<class>a.Class1</class>
<class>a.Class2</class>
<class>a.Class3</class>
</persistence-unit>
<persistence-unit name="projectB" ...>
...
<class>a.Class1</class>
<class>a.Class2</class>
<class>a.Class3</class>
<class>b.Class1</class>
<class>b.Class2</class>
<class>b.Class3</class>
</persistence-unit>
</persistence>
Alternatively, you can use the <jar-file> element, quoting from the JPA spec (6.2.1.6): "If specified, these JAR files will be searched for managed persistence classes, and any mapping metadata annotations found on them will be processed, or they will be mapped using the mapping annotation defaults defined by this specification. Such JAR files are specified relative to the root of the persistence unit (e.g., utils/myUtils.jar)."
<persistence ...>
<persistence-unit name="projectA" ...>
...
<jar-file>relative/path/to/your/library.jar</jar-file>
</persistence-unit>
</persistence>
Related
In my project i have a persistence bundle which will perform the create/update/delete operations and multiple model bundles.
persistence bundle:
PersistenceService.java - Exposed as a service.
PersistenceServiceImpl.java
persistence.xml - contains:
a: jta-data-source details
b: dialect properties.
Now i have multiple model bundles where i have used annotations for declaring entities.
My query is, how i can specify the different model bundles details in the above mentioned persistence bundle's persistence.xml. so that the i can maintain a single persistence.xml file and "PersistenceUnitInfo" will not be repeated.
is it possible? or is there any other best practices?
I am a new OpenJPA 2.2.2 user. I noticed that the database configuration has to be defined in META-INF/persistence.xml. This sounds to be too inflexible. Moreover, I found from the OpenJPA 2.2.2 documentation:
The OpenJPA runtime includes a comprehensive system of configuration defaults and overrides:
OpenJPA first looks for an optional openjpa.xml resource. OpenJPA searches for this resource in each top-level directory of your CLASSPATH. OpenJPA will also find the resource if you place it within a META-INF directory in any top-level directory of the CLASSPATH. The openjpa.xml resource contains property settings in JPA's XML format.
You can customize the name or location of the above resource by specifying the correct resource path in the openjpa.properties System property.
On base the introduction above, it seems that the database configuration file name is only possible to be persistence.xml. Moreover, I tried to place it in the top-level directory of my CLASSPATH, i.e., not inside the META-INF directory, it didn't work at all! Is there is any way to define this database persistence xml more flexibly, say in somewhere else than META-INF and with other name than persistence.xml?
First, there is no replacement for persistence.xml (p.xml for short). This is needed no matter what. Furthermore, the location of this file is clearly defined in the JPA specification (e.g. see "8.2 Persistence Unit Packaging" in JPA 2.0 spec). You MUST follow those rules on packaging. OpenJPA first and foremost follows the rules of the spec in regards to this file and its settings. No JPA provider is going to ignore the rules w.r.t p.xml file.
The section of text you copy/pasted is from here in the OpenJPA documentation:
http://openjpa.apache.org/builds/2.2.2/apache-openjpa/docs/manual#ref_guide_conf_specify
As you can see, this section describes an optional file named openjpa.xml. This file does not replace a p.xml file! It adds to it. And as the documentation states, it is simply used to add properties. That is, you can not define your persistence unit here, the only thing you can define are properties (i.e. ). If you define a persistence unit here, it will not be used. As an example, lets look at this openjpa.xml file:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="">
<properties>
<property name="openjpa.jdbc.Schema" value="MySchema" />
<property name="openjpa.jdbc.DBDictionary" value="db2" />
</properties>
</persistence-unit>
This file defines two properties. Notice that the persistence-unit name is blank. That is intentional because, as I mentioned above, we can't define a persistence unit in this file. Finally, you can place this file in the META-INF on your classpath, as mentioned in the documentation. If you put this at the same spot as your p.xml file it will be found. As an example, in my JSE JUnit test, I place it on my hard drive in the directory e:/openjpaConfig/META-INF/openjpa.xml. I then put a classpath entry in my JUnit test to point to the directory e:/openjpaConfig. The classpath could point to a jar which contains this file in its META-INF directory. In a Java EE environment, you could place the file in a .jar file and place the .jar file in the lib directory of an ear.
I have a java-ee web application that uses a persistence unit that is packaged as a jar dependency (Entity classes, EJB repositories, persistence.xml).
In order to get some acceptance tests running for the web application i need to override a property in the packaged persistence.xml. To be specific i need to disable the by default active eclipselink shared object cache by setting the following property.
<property name="eclipselink.cache.shared.default" value="false"/>
This is necessary because the acceptance tests are directly prepare/cleanup the database with dbunit. These modifications will put the eclipselink cache in a stale state (because the persistence unit is not involved in these modifications).
Is there a way in java-ee (or glassfish specific) to override properties in a persistence.xml that is located in a jar (starting from the web application war file, that is deployed when running my tests)?
There may be other ways, for example building the jar dependency specific for a test deployment, but this route seems complicated to me for only override one property in my persistence.xml.
You can pass a properties map to Persistence.createEntityManagerFactory(). To do this you must manage your persistence context yourself (will not be able to inject it).
Another option is to set the property as a Java system property (-D=), this will not override an existing property in the persistence.xml, but with work if the property is not in the persistence.xml.
Another option is to put a SessionCustomizer or a SessionTuner in your persistence.xml to allow your own code to modify the configuration at runtime.
I want to split parts of my WAR application into different JAR components.
Every JAR component contains JPA-Entities, EJBs & JSF-Composite-Components.
Example: extracting the user management (XHTMLs, EJBs, JPA-Entities) into an own jar.
At first everything looks working fine as long as I don't have to use an entityManager.
Problem:
In EJBs of JAR-files the entityManager will never be injected.
I'm using the #PersistenceContext annotation for injection.
I have a beans.xml in all META-INF folders and everything (excepted entityManager) is injected correctly. It doesn't even matter if I place a persistence.xml in all JAR-files or only in the WAR-file.
Does anybody have a glue how this can be done?
Where do I have to place the different configuration files (beans.xml, persistence.xml)?
Do I need an ejb-jar.xml file?
Is this possible at all?
Technology-Stack:
JBoss 6.1 / PrimeFaces 3.2 / Maven / EJB 3.1 / Hibernate / JTA / CDI
You need a persistence.xml file in every jar that contains entities.
Every such persistence xml should define a different persistence unit.
If you want one persistence unit to refer to entities define in a different persistence unit then you need to include it as jar-file entry in the persistence unit definition (see example below).
This works for us for a very similar stack JBoss 5.1 (and then 7)/ EJB3/ Hibernate, Maven, ...
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="PersistenceUnitA">
<jar-file>JarContainingOtherPersistenceUnit</jar-file>
<jta-data-source>java:/jboss-mysql-ds</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>
What is the value to be set in provider XML element of persistence.xml if the Oracle database is used? Which jars need to included to write a simple JPA application?.
I have currently included only ejb3-persistence.jar.
When the application is run, below error is seen,
javax.persistence.PersistenceException: No Persistence provider for EntityManage
Decide which JPA implementation you're going to use :- some to choose from DataNucleus, OpenJPA, Hibernate, EclipseLink all of which support persistence to Oracle RDBMS, and there are likely others. Each has a "provider" class name, so you use that in persistence.xml