Persistence.xml cheatsheet

Need to setup a standalone persistence.xml for your application? then the following cheatsheet could help you!

<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">

	<!-- hsqldb -->

	<persistence-unit name="hsqldb">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class" />
			<property name="hibernate.dialect" value="com.mysema.query.jpa.support.ExtendedHSQLDialect" />
			<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
			<property name="hibernate.connection.url" value="jdbc:hsqldb:file:target/testdb;shutdown=true" />
			<property name="hibernate.connection.user" value="sa" />
			<!-- <property name="hibernate.show_sql" value="true"/> -->
			<property name="hibernate.flushMode" value="FLUSH_AUTO" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

	<persistence-unit name="hsqldb-eclipselink"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:target/testdb2;shutdown=true" />
			<property name="javax.persistence.jdbc.user" value="sa" />
			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
			<property name="eclipselink.ddl-generation.output-mode" value="database" />
		</properties>
	</persistence-unit>

	<!-- h2 -->

	<persistence-unit name="h2" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
			<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
			<property name="hibernate.connection.url" value="jdbc:h2:target/h2-1" />
			<property name="hibernate.connection.user" value="sa" />
			<!-- <property name="hibernate.show_sql" value="true"/> -->
			<property name="hibernate.flushMode" value="FLUSH_AUTO" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

	<persistence-unit name="h2-eclipselink"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:h2:target/h2-2" />
			<property name="javax.persistence.jdbc.user" value="sa" />
			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
			<property name="eclipselink.ddl-generation.output-mode" value="database" />
		</properties>
	</persistence-unit>

	<persistence-unit name="h2-openjpa" transaction-type="RESOURCE_LOCAL">
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:h2:target/h2-3" />
			<property name="javax.persistence.jdbc.user" value="sa" />
			<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
			<!-- <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> 
			<property name="eclipselink.ddl-generation.output-mode" value="database" 
				/> -->
		</properties>
	</persistence-unit>

	<!-- mysql -->

	<persistence-unit name="mysql">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/querydsl" />
			<property name="hibernate.connection.username" value="querydsl" />
			<property name="hibernate.connection.password" value="querydsl" />
			<!-- <property name="hibernate.show_sql" value="true"/> -->
			<property name="hibernate.flushMode" value="FLUSH_AUTO" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

	<persistence-unit name="mysql-eclipselink"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/querydsl" />
			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
			<property name="eclipselink.ddl-generation.output-mode" value="database" />
		</properties>
	</persistence-unit>

	<!-- postgres -->

	<persistence-unit name="postgres">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
			<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
			<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/querydsl" />
			<property name="hibernate.connection.username" value="querydsl" />
			<property name="hibernate.connection.password" value="querydsl" />
			<!-- <property name="hibernate.show_sql" value="true"/> -->
			<property name="hibernate.flushMode" value="FLUSH_AUTO" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

	<!-- oracle -->

	<persistence-unit name="oracle">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class" />
			<property name="hibernate.dialect" value="com.mysema.query.jpa.support.ExtendedOracleDialect" />
			<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
			<property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
			<property name="hibernate.connection.username" value="querydsl" />
			<property name="hibernate.connection.password" value="querydsl" />
			<!-- <property name="hibernate.show_sql" value="true"/> -->
			<property name="hibernate.flushMode" value="FLUSH_AUTO" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

	<!-- derby -->

	<persistence-unit name="derby">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class" />
			<property name="hibernate.dialect" value="com.mysema.query.jpa.support.ExtendedDerbyDialect" />
			<property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.EmbeddedDriver" />
			<property name="hibernate.connection.url" value="jdbc:derby:target/derbydb;create=true" />
			<!-- <property name="hibernate.show_sql" value="true"/> -->
			<property name="hibernate.flushMode" value="FLUSH_AUTO" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

	<persistence-unit name="derby-eclipselink"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:derby:target/derbydb2;create=true" />
			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
			<property name="eclipselink.ddl-generation.output-mode" value="database" />
		</properties>
	</persistence-unit>

	<persistence-unit name="derby-openjpa"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:derby:target/derbydb3;create=true" />
			<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
			<!-- <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> 
			     <property name="eclipselink.ddl-generation.output-mode" value="database" /> -->
		</properties>
	</persistence-unit>

      <!-- DB2 -->

	<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_1_0.xsd">

	   <persistence-unit name="OrderJPA">
	     <class>com.ibm.issw.order.entities.Address</class>
	     <class>com.ibm.issw.order.entities.Customer</class>
     	     <class>com.ibm.issw.order.entities.Orderinfo</class>
             <class>com.ibm.issw.order.entities.Orderitem</class>
     		<properties>
      			<property name="openjpa.ConnectionURL"  
       				value="jdbc:db2://localhost:50000/ORDERDB" />
      			<property name="openjpa.ConnectionDriverName" 
				value="com.ibm.db2.jcc.DB2Driver" />
      			<property name="openjpa.ConnectionUserName" value="db2admin" />
      			<property name="openjpa.ConnectionPassword" value="db2admin"/>
      			<property name="openjpa.Log" value="SQL=TRACE"/>
     		</properties>
    	</persistence-unit>
</persistence>

 

How to use JPA from a JBoss Web application ?

JBoss recipe of the day

Read more: How to use JPA from a JBoss Web application ?

How to use multiple database in persistence.xml?

The Java Persistence API allows you to define multiple persistence units, each of which can map to a separate database. If you look at the persistence.xml file :

   <persistence>
    <persistence-unit name="sample-db1">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <jta-data-source>jdbc/SamplesDB</jta-data-source>
    </persistence-unit>
    <persistence-unit name="sample-db2">
     <provider>
      oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
     </provider>
     <jta-data-source>jdbc/SamplesDB2</jta-data-source>
    </persistence-unit>
   </persistence>

The sample-db1 and sample-db2 persistence units are used to create and inject the appropriate entity managers into your EJB. Here is the code in the session bean that injects the entity managers with a transactional persistence context:

   @PersistenceContext(unitName="sample-db1", 
          type=PersistenceContextType.TRANSACTION)
   private EntityManager em1;
       
   @PersistenceContext(unitName="sample-db2", 
          type=PersistenceContextType.TRANSACTION)
   private EntityManager em2;

How to deploy the Persistence unit in the global JNDI ?

By default the persistence unit are available in the java: Context. If you wish to make them available also in the global naming Context you have to add two properties to your persistence.xml configuration file:

<persistence>
   <persistence-unit name="manager1">
      <jta-data-source>java:/MySQLDS</jta-data-source>
      <properties>
         <property name="jboss.entity.manager.jndi.name" value="java:/Manager1"/>
         <property name="jboss.entity.manager.factory.jndi.name" value="java:/Manager1Factory"/>
      </properties>
   </persistence-unit>
</persistence>

jboss.entity.manager.jndi.name gives you a transaction scoped entity manager you can interact with, while jboss.entity.manager.factory.jndi.name binds the entity manager factory into global JNDI.

How to use an env variable in your persistence.xml ?

In this short tutorial we will show how to use variables in your persistence.xml so that you can dynamically specify the data source which is used by our application. 

Read more: How to use an env variable in your persistence.xml ?

Follow us on Twitter