This is a handy cheatsheet for JPA developers with a collection of most common persistence.xml configurations.
How to define JDBC Connection settings in persistence.xml
<persistence-unit name="my-pu"> <description>My Persistence Unit</description> <properties> <property name="javax.persistence.jdbc.url" value="JDBC URL"/> <property name="javax.persistence.jdbc.user" value="user"/> <property name="javax.persistence.jdbc.password" value="password"/> </properties> </persistence-unit>
How to declare external Entity classes in persistence.xml
<persistence-unit name="my-pu"> <description>My Persistence Unit</description> <provider>com.objectdb.jpa.Provider</provider> <class>sample.MyEntity1</class> <class>sample.MyEntity2</class> </persistence-unit>
How to declare external Entity classes from a JAR file
<persistence-unit name="my-pu"> <description>My Persistence Unit</description> <provider>com.objectdb.jpa.Provider</provider> <jar-file>packedEntity.jar</jar-file> </persistence-unit>
How to exclude from scanning Entity classes non listed in persistence.xml
<persistence-unit name="mysql-eclipselink" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <exclude-unlisted-classes>true</exclude-unlisted-classes> </persistence-unit>
Finally, here is a cheatsheet with some examples of persistence.xml.
<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>