Chapter 4. Configuring Hibernate for Red Hat JBoss Web Server

Hibernate ORM is an object-relational mapping framework that lets you connect JBoss Web Server to JDBC datasources.

4.1. Installing Hibernate ORM

Complete the following procedure to install Hibernate ORM on all platforms that JBoss Web Server supports.

Prerequisites

Configure your project to use the JBoss Web Server Maven Repository, which is available to download as jboss-web-server-5.3.0-maven-repository.zip.

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.

4.2. Configuring JDBC Connection Pools

Tomcat provides a default connection pooling mechanism for JDBC datasources.

Procedure

  1. Open your deployment’s /META-INF/context.xml file for editing.
  2. Modify the JDBC connection pools available to applications, as in the following 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>

4.3. Configuring Hibernate Connection Properties

Configure Hibernate to use connections from the Tomcat pool. If you use the Hibernate API directly, use a similar configuration to hibernate.cfg.xml.

Procedure

  1. Open your deployment’s /WEB-INF/classes/META-INF/persistence.xml file for editing.
  2. Configure how Hibernate consumes connections from the Tomcat, as in the following 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>

4.4. Adding JDBC Data Sources

Configure Tomcat to consume JDBC datasources.

Procedure

  1. Open your deployment’s /WEB-INF/web.xml file for editing.
  2. Configure JDBC datasources with the resource-env-ref element, as in the following example that uses a jdbc/DsWebAppDB datasource:

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