JPA 2.0 : standard properties in persistence.xml

As you might know with JPA, the META-INF/persistence.xml file defines a persistence unit with some provider’s properties. For example, if you are using JPA in a Java SE environment, you will have to define the JDBC driver, database connexion (user and password), database URL and so forth. In JPA 1.0 these properties were not standard, so for each persistence provider you would have to use proprietary properties. For example, with EclipseLink this is what you would need to connect to a Derby database :

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  <persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.apress.javaee6.chapter02.Book</class>
    <properties>
      <property name="eclipselink.target-database" value="DERBY"/>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
      <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
      <property name="eclipselink.jdbc.user" value="APP"/>
      <property name="eclipselink.jdbc.password" value="APP"/>
    </properties>
  </persistence-unit>
</persistence>

As you can see, each property is called eclipselink.something. With the new JPA 2.0 shipping with Java EE 6 in September, some properties have been standardised :

  • javax.persistence.jdbc.driver — fully qualified name of the driver class
  • javax.persistence.jdbc.url — driver-specific URL
  • javax.persistence.jdbc.user — username used by database connection
  • javax.persistence.jdbc.password — password for database connection validation

If you use the latest build of EclipseLink (reference implementation of JPA 2.0) you will be able to use these properties as follow :

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

  <persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.apress.javaee6.chapter02.Book</class>
    <properties>
      <property name="eclipselink.target-database" value="DERBY"/>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
      <property name="javax.persistence.jdbc.user" value="APP"/>
      <property name="javax.persistence.jdbc.password" value="APP"/>
    </properties>
  </persistence-unit>
</persistence>

Except for the target-database and ddl-generation properties, the rest of the persistence.xml file is portable across implementations. Again, this is just an example showing how Java EE 6 is trying to make code as portable as possible. If you want to know more about it, check the JPA 2.0 specification.

About these ads
Comments
2 Responses to “JPA 2.0 : standard properties in persistence.xml”
  1. I’m getting an access violation for derby.log when I try to run the test in chapter 2 of your Java EE 6 Book. I have checked and the directory and file are all r/w for everyone!

    Mon Mar 21 18:25:31 EDT 2011 : Security manager installed using the Basic server security policy.
    Mon Mar 21 18:25:31 EDT 2011 Thread[main,5,main] java.security.AccessControlException: access denied (java.io.FilePermission derby.log read)
    Mon Mar 21 18:25:31 EDT 2011 : Apache Derby Network Server – 10.7.1.1 – (1040133) started and ready to accept connections on port 1527
    Mon Mar 21 18:25:31 EDT 2011 : Apache Derby Network Server – 10.7.1.1 – (1040133) started and ready to accept connections on port 1527

    Is there a way to add something to the persistence.xml to fix this or is the problem something else. The test succeeds but actually throws an exception and then says the table “BOOK” does not exist when it tries to DROP the table.

  2. Kris says:

    Nice article

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 6,112 other followers

%d bloggers like this: