3. JPA Configuration

Hibernate is setup as the JPA provider. Another JPA provider, like EclipseLink, could be specified and if all your code just uses JPA nothing else would need to be changed.

The JPA implementation is specified using the provider element. The Person & Address classes are explicitly configured, and JPA's scanning for entity beans is turned off by specifying the exclude-unlisted-classes element. This is mainly because there are multiple JPA entities in one project for these examples, otherwise having automatic scanning would normally be fine.

The properties element can have a list of properties to pass into JPA provider for configuring provider specific details. In this case the database being used, whether or not to show generated SQL, whether or not to to generate the schema or validate an existing one, the naming strategy, and the cache provider.


In production it would be better to use Ehcache for the cache provider.

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />

Excerpt from META-INF/persistence.xml

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

    <persistence-unit name="simple-jpa">

            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />