JPA - persistence.xml

> JPA - Java Persistence API (JPA)

1 - About

The JPA specification requires the use of a persistence.xml file for deployment.

The persistence configuration file must be named “META-INF/persistence.xml” in the persistence archive.

This file defines one or more persistence unit where:

The Xsd for the persistence configuration file can be found:

Advertising

3 - Schema

Persistence configuration files must indicate

  • the persistence schema by using the persistence namespace:

http://java.sun.com/xml/ns/persistence

  • and indicate the version of the schema by using the version element as shown below:
<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_2_0.xsd"
             version="2.0">
...
</persistence>

4 - Node

4.1 - Provider

The provider node define the entity managers class. The provider element is optional but should be specified if the application is dependent upon a particular persistence provider being used. The class must be present in the classpath.

For EclipseLink:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

For Hibernate:

<provider>org.hibernate.ejb.HibernatePersistence</provider>

For Toplink:

<provider>oracle.toplink.essentials.PersistenceProvider</provider>
Advertising

4.2 - exclude-unlisted-classes

<exclude-unlisted-classes>false</exclude-unlisted-classes>

All the class that are unlisted will/or not be automatically discovered with reflection. Ie if the @Entity annotations is present, the class is an entity.

5 - Example

5.1 - SE

Eclipselink configuration for a derby database with Logging level as properties

<?xml version="2.0" encoding="UTF-8"?>
<persistence version="1.0" 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_2_0.xsd">
  <persistence-unit name="TroubleTicketSystemServer" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.oracle.ticketsystem.beans.Tickethistory</class>
	<class>com.oracle.ticketsystem.beans.Ticket</class>
	<class>com.oracle.ticketsystem.beans.Technicianprivatedata</class>
	<class>com.oracle.ticketsystem.beans.Product</class>
	<class>com.oracle.ticketsystem.beans.Department</class>
	<class>com.oracle.ticketsystem.beans.Technician</class>
    <properties>
      <property name="eclipselink.target-database" value="Derby"/>            
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/jpatutorial;create=true"/>
      <property name="javax.persistence.jdbc.user" value="app"/>
      <property name="javax.persistence.jdbc.password" value="app"/>
      <property name="eclipselink.logging.level" value="ALL"/>            
    </properties>
  </persistence-unit>
</persistence>

Add the derby jars to the application classpath

5.2 - EE

<persistence version="2.0" 
    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_2_0.xsd">
    <persistence-unit name="example" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>localJTA</jta-data-source>
        <!-- optional --><class>org.eclipse.persistence.example.business.Cell</class>
        <properties>
            <!-- optional --><property name="eclipselink.target-database" 
                      value="org.eclipse.persistence.platform.database.DerbyPlatform"/>
            <property name="eclipselink.target-server" value="WebLogic_10"/>
            <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>       
    </persistence-unit>
</persistence>

Add derby jars to the application server classpath (Example: for WebLogic place in the modules directory)

6 - Support

6.1 - No Persistence provider for EntityManager

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named myProviderUnit:  
The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
	at test.Main.main(Main.java:51)

No persistence provider node with the name myProviderUnit can be found.

Advertising

7 - Documentation / Reference

jpa/persistence.xml.txt · Last modified: 2017/01/20 18:12 by gerardnico