Chapter 2. Hibernate Configuration 2.1. Hibernate Configuration The configuration for entity managers both inside an application server and in a standalone application reside in a persistence archive. A persistence archive is a JAR file which must define a persistence.xml file that resides in the META-INF/ folder. You can connect to the database using the persistence.xml file. There are two ways of doing this: Specifying a data source which is configured in the datasources subsystem in JBoss EAP. The jta-data-source points to the Java Naming and Directory Interface name of the data source this persistence unit maps to. The java:jboss/datasources/ExampleDS here points to the H2 DB embedded in the JBoss EAP. Example of object-relational-mapping in the persistence.xml File <persistence> <persistence-unit name="myapp"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> <properties> ... ... </properties> </persistence-unit> </persistence> Explicitly configuring the persistence.xml file by specifying the connection properties. Example of Specifying Connection Properties in the persistence.xml file <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/> <property name="javax.persistence.jdbc.user" value="sa"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:."/> For the complete list of connection properties, see Connection Properties Configurable in the persistence.xml File . There are a number of properties that control the behavior of Hibernate at runtime. All are optional and have reasonable default values. These Hibernate properties are all used in the persistence.xml file. For the complete list of all configurable Hibernate properties, see Hibernate Properties . 2.2. Second-Level Caches 2.2.1. About Second-level Caches A second-level cache is a local data store that holds information persisted outside the application session. The cache is managed by the persistence provider, improving runtime by keeping the data separate from the application. JBoss EAP supports caching for the following purposes: Web Session Clustering Stateful Session Bean Clustering SSO Clustering Hibernate Second-level Cache Jakarta Persistence Second-level Cache Warning Each cache container defines a repl and a dist cache. These caches should not be used directly by user applications. 2.2.2. Configure a Second-level Cache for Hibernate The configuration of Infinispan to act as the second-level cache for Hibernate can be done in two ways: It is recommended to configure the second-level cache through Jakarta Persistence applications, using the persistence.xml file , as explained in the JBoss EAP Development Guide . Alternatively, you can configure the second-level cache through Hibernate native applications, using the hibernate.cfg.xml file, as explained below. Configuring a Second-level Cache for Hibernate Using Hibernate Native Applications Create the hibernate.cfg.xml file in the deployment's class path. Add the following XML to the hibernate.cfg.xml file. The XML needs to be within the <session-factory> tag: <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.region.factory_class">org.jboss.as.jpa.hibernate5.infinispan.InfinispanRegionFactory</property> In order to use the Hibernate native APIs within your application, you must add the following dependencies to the MANIFEST.MF file:

Commands:
<persistence> <persistence-unit name="myapp"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> <properties> ... </properties> </persistence-unit> </persistence>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/> <property name="javax.persistence.jdbc.user" value="sa"/> <property name="javax.persistence.jdbc.password" value=""/> <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:."/>
<property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.region.factory_class">org.jboss.as.jpa.hibernate5.infinispan.InfinispanRegionFactory</property>
Dependencies: org.infinispan,org.hibernate