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.

[Note]Note

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

    ...
    
    <persistence-unit name="simple-jpa">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>org.springbyexample.orm.jpa.bean.Person</class>
        <class>org.springbyexample.orm.jpa.bean.Address</class>
        
        <exclude-unlisted-classes/>
        
        <properties>
            <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" />
        </properties>
    </persistence-unit>

    ...
    
</persistence>