Este conteúdo não está disponível no idioma selecionado.

Chapter 5. Configuring Hibernate for JBoss Web Server


Hibernate Object/Relational Mapping (ORM) is an object-relational mapping framework that you can use to connect JBoss Web Server to Java database connectivity (JDBC) datasources. You can use Hibernate ORM with JBoss Web Server when you want to store your application data in a relational database.

5.1. Installing Hibernate ORM

You can install Hibernate ORM on all platforms that JBoss Web Server supports.

Prerequisites

  • You have configured your project to use the JBoss Web Server Maven Repository.

    Note

    The JBoss Web Server Maven Repository is available to download as jboss-web-server-5.6.0-maven-repository.zip. from the Red Hat Software Downloads page.

Procedure

  1. Get the Hibernate JAR files from the JBoss Web Server Maven Repository.
  2. Add the Hibernate JAR files to your deployment WAR file.

5.2. Configuring JDBC connection pools

Apache Tomcat provides a default connection pooling mechanism for JDBC datasources. You can configure JDBC connection pools by updating settings in the /META-INF/context.xml file for your JBoss Web Server deployment.

Procedure

  1. Open the /META-INF/context.xml file.
  2. Modify the JDBC connection pools that are available to applications.

    For example:

    <Context>
      <Resource
        name="jdbc/DsWebAppDB"
        auth="Container"
        type="javax.sql.DataSource"
        username="sa"
        password=""
        driverClassName="org.h2.Driver"
        url="jdbc:h2:mem:target/test/db/h2/hibernate"
        maxActive="8"
        maxIdle="4"/>
    </Context>
    Copy to Clipboard Toggle word wrap

5.3. Configuring Hibernate connection properties

You can configure Hibernate to use JDBC connections from the Tomcat pool, by updating settings in the /WEB-INF/classes/META-INF/persistence.xml file for your JBoss Web Server deployment.

Note

If you want to use the Hibernate API directly, use a similar configuration to the hibernate.cfg.xml file.

Procedure

  1. Open the /WEB-INF/classes/META-INF/persistence.xml file.
  2. Configure Hibernate to consume connections from Tomcat.

    For example:

    <persistence version="1.0"
      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">
    
      <persistence-unit name="dswebapp">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
          <property name="hibernate.connection.datasource" value="java:comp/env/jdbc/DsWebAppDB"/>
        </properties>
      </persistence-unit>
    </persistence>
    Copy to Clipboard Toggle word wrap

5.4. Adding JDBC data sources

You can configure Tomcat to consume JDBC data sources, by updating settings in the /WEB-INF/web.xml file for your JBoss Web Server deployment.

Procedure

  1. Open the /WEB-INF/web.xml file.
  2. Configure JDBC datasources with the resource-env-ref element.

    For example:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
     <resource-env-ref>
      <resource-env-ref-name>jdbc/DsWebAppDB</resource-env-ref-name>
      <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
     </resource-env-ref>
    </web-app>
    Copy to Clipboard Toggle word wrap
    Note

    The preceding example uses a a jdbc/DsWebAppDB data source.

Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat