JPA - persistence.xml

Card Puncher Data Processing

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:

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>

Node

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>

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.

Example

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

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)

Support

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.

Documentation / Reference





Discover More
Java Conceptuel Diagram
J2EE - JPA and Stateless Session Bean (EJB) as Data Access Object (DAO)

Stateless EJB session beans as Data Access Object (DAO) implementation with JPA. You must use the EntityManagerFactory to get an EntityManager The resulting EntityManager instance is a PersistenceContext/Cache...
Card Puncher Data Processing
JPA - Entity (Managed Classes)

xmlparserv2.jar Introduction to EclipseLink JPA (ELUG) for more information on the annotation.
Card Puncher Data Processing
JPA - Getting Started - Hello World

A simple JPA implementation that insert an “Hello World” string in a HELLO table in a relational database. This example uses the JPA reference implementation: against an Oracle Database. Maven...
One To Many Hello Data Model
JPA - How to define a @One-to-Many relationship ?

To define a one-to-many relationship, the following annotation is used @OneToMany. The Interface with parameters and default values: where: targetEntity is the entity class that is the target...
Hello Data Model Many To Many
JPA - How to model a @Many-to-Many Relationship ?

How to model a many-to-many relationship in JPA. One Hello can have several Category and One Category can have several Hello. The Ddl File: [Ddl File] One sequence: The HelloWorld entity...
Card Puncher Data Processing
JPA - Managed Class

entities, mapped superclasses, and embedded...
Card Puncher Data Processing
JPA - Open JPA

Persistence.Xml Provider: Persistence Properties: Persistence.Xml Dynamically
Card Puncher Data Processing
JPA - Persistence Properties

persistence.properties are database and JPA provider specific. You can configure a properties (such as database connections) using: a properties file, an XML deployment descriptor (persistence.xml)...
Card Puncher Data Processing
JPA - Persistence Unit

the node provider property See chapter 8.1 of the specification
Card Puncher Data Processing
JPA - orm.xml (Mapping Descriptor files)

orm.xml is a deployment descriptors describing the object-relational mapping. The Xsd Schema file can be found: orm_2_0.xsd or in the specification...



Share this page:
Follow us:
Task Runner