此内容没有您所选择的语言版本。

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 JNDI 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.ejb.HibernatePersistence</provider>
          <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
          <properties>
             ... ...
          </properties>
       </persistence-unit>
    </persistence>
    Copy to Clipboard Toggle word wrap

  • 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:."/>
    Copy to Clipboard Toggle word wrap

    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/JPA 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 JPA 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
  1. Create the hibernate.cfg.xml file in the deployment’s class path.
  2. 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>
    Copy to Clipboard Toggle word wrap
  3. In order to use the Hibernate native APIs within your application, you must add the following dependencies to the MANIFEST.MF file:

    Dependencies: org.infinispan,org.hibernate
    Copy to Clipboard Toggle word wrap
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部