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
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
datasourcessubsystem in JBoss EAP.
jta-data-sourcepoints to the Java Naming and Directory Interface name of the data source this persistence unit maps to. The
java:jboss/datasources/ExampleDShere points to the
H2 DBembedded in the JBoss EAP.
<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.xmlfile by specifying the connection properties.
Example of Specifying Connection Properties in the
<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
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
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.xmlfile, as explained in the JBoss EAP Development Guide.
Alternatively, you can configure the second-level cache through Hibernate native applications, using the
hibernate.cfg.xmlfile, as explained below.
Configuring a Second-level Cache for Hibernate Using Hibernate Native Applications
hibernate.cfg.xmlfile in the deployment’s class path.
Add the following XML to the
hibernate.cfg.xmlfile. The XML needs to be within the
<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