The Source for Java Technology Collaboration

As the new Java.net infrastructure contains project-level wikis, this main wiki will be shut down in the near future. For wiki page export and general wiki questions please contact the site admin at communitymanager@java.net.

Using Hibernate with SJSAS 8.1 PE (Sun Java System Application Server)

Hibernate is a Java based object/relational persistence and query service. This tutorial describes how to access and use Hibernate functions from within a J2EE application deployed on Sun's application server.

First of all, this tutorial assumes that the reader is familiar with using Hibernate classes from within a Java application. This tutorial focuses on how to setup and use Hibernate from within a J2EE compatible application on Sun's application server (SJSAS 8.1 PE). As with any tutorial, this showcases just one way of accomplishing the task at hand - there are other ways to make this work as well.

Example 1: J2EE EAR file containing an EJB-JAR.

1 Download the hibernate jars from (http://www.hibernate.org/30.html). This tutorial uses Hibernate 2.0. The download contains hibernate jars along with other dependent jar files, that are needed at runtime. We will discuss the dependent jars a bit later.

2 The EJB code uses hibernate classes as shown in the psuedo code below. Compiling this code would require hibernate2.jar. In this example, Customer is the POJO (Plain Old Java Object) that is mapped to a table CUSTOMER in a database.

// Sample psuedo code.

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class .... {

    private void testHibernate() throws HibernateException {
        SessionFactory sessionFactory = // this should ideally be a singleton
            new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx= session.beginTransaction();
        Customer customer = new Customer();
        customer.setName("Raghavan");
        session.save(customer);
        tx.commit();
        session.close();
    }

    public void foo() { // remote method
        testHibernate();
        ...;
    }
}

public class Customer { // POJO

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    private void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3 Create a hibernate configuration file hibernate.cfg.xml, as shown below, and edit relevant properties, including the datasource. Note, there are other ways to supply configuration information to Hibernate runtime. Please refer to http://www.hibernate.org/ for details.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/jdbc/__Hibernate</property>
        <property name="show_sql">false</property>
        <property name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Mapping files -->
        <mapping resource="Customer.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

4. Create a JDBC resource (jdbc/__Hibernate, for this example) as follows:

# Go to application server installation directory, and into the bin folder.

$ cd $(S1AS_HOME)/bin

# Create a JDBC connection pool. We are using Pointbase database in this example. You may use other databases.

$ ./asadmin create-jdbc-connection-pool --datasourceclassname=com.pointbase.xa.xaDataSource --restype javax.sql.XADataSource --property
User=PBPUBLIC:Password=pbpublic:DatabaseName="jdbc\:pointbase\:server\://localhost\:9092/sample" HibernatePool

# Create a JDBC resource.

./asadmin create-jdbc-resource --connectionpoolid HibernatePool jdbc/__Hibernate

5. Create a Customer.hbm.xml mapping file as shown below.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

    <class name="com.sun.s1peqe.ejb.stateless.converter.ejb.Customer" table="CUSTOMER">

        <!-- A 32 hex character is our surrogate key. It's automatically
            generated by Hibernate with the UUID pattern. -->
        <id name="id" type="string" unsaved-value="null" >
            <column name="CUSTOMER_ID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
        </id>

        <!-- A customer has to have a name, but it shouldn't be too long. -->
        <property name="name">
            <column name="NAME" length="16" not-null="true"/>
        </property>

    </class>

</hibernate-mapping>

6. Edit the application's ejb-jar.xml and sun-ejb-jar.xml files using the following example as a guide. The element of interest is the resource-ref setting.

ejb-jar.xml:

<ejb-jar>
  <enterprise-beans>
    <session>
      <display-name>WombatApp</display-name>
      <ejb-name>WombatBean</ejb-name>
      <home>com.wombat.ejb.WombatRemoteHome</home>
      <remote>com.wombat.ejb.WombatRemote</remote>
      <ejb-class>com.wombat.ejb.WombatBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Bean</transaction-type>
      <resource-ref>
          <res-ref-name>jdbc/__Hibernate</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
    </session>
  </enterprise-beans>
</ejb-jar>

sun-ejb-jar.xml:

<sun-ejb-jar>
  <enterprise-beans>
    <unique-id>1</unique-id>
    <ejb>
      <ejb-name>WombatBean</ejb-name>
      <jndi-name>Wombat</jndi-name>
      <resource-ref>
          <res-ref-name>jdbc/__Hibernate</res-ref-name>
          <jndi-name>jdbc/__Hibernate</jndi-name>
      </resource-ref>
    </ejb>
  </enterprise-beans>
</sun-ejb-jar>

7. Edit the EJB-JAR's META-INF/MANIFEST.MF files as follows, and include a Class-Path entry containing a space separated list of hibernate and dependent jar files. All these jar files are available as part of the hibernate download.

Manifest-Version: 1.0
Class-Path: hibernate2.jar dom4j-1.4.jar jta.jar log4j-1.2.8.jar cglib-full-2.0.2.jar odmg-3.0.jar ehcache-0.9.jar commons-collections-2.1.1.jar commons-dbcp-1.2.1.jar commons-lang-1.0.1.jar commons-logging-1.0.4.jar commons-pool-1.2.jar
Ant-Version: Apache Ant 1.5.4
Created-By: 1.5.0_01-b08 (Sun Microsystems Inc.)

8. Package the EJB-JAR such that it contains the following:
META-INF/MANIFEST.MF
META-INF/ejb-jar.xml
META-INF/sun-ejb-jar.xml
<application classes>
hibernate2.jar
Customer.hbm.xml
hibernate.cfg.xml
dom4j-1.4.jar
jta.jar
log4j-1.2.8.jar
cglib-full-2.0.2.jar
odmg-3.0.jar
ehcache-0.9.jar
commons-collections-2.1.1.jar
commons-dbcp-1.2.1.jar
commons-lang-1.0.1.jar
commons-logging-1.0.4.jar
commons-pool-1.2.jar

9. Package the EJB-JAR, as you would normally, in an EAR. Deploy the EAR.

10. Manually create a table CUSTOMER in the database. In this example, we used a pointbase database named jdbc:pointbase:server://localhost:9092/sample". Refer to step 4. Of course, you can use other database names and database systems, but then, step 4 should be configured appropriately with the correct database name. For database interaction, you may use the tool $(S1AS _HOME)/pointbase/tools/serveroption/startconsole.sh.

11. Edit the server.policy file in the server's installation directory, and include a codebase permission entry for the hibernate classes.

12. Deploy the EAR, and run a client application that invokes the EJB. The EJB would internally access Hibernate to persist the Customer object. You may verify the database table, and find that a new row has been inserted into the CUSTOMER table, if everything completes successfully.

Example 2: J2EE EAR containing a Web application.

This is similar to example 1, except that instead of EJB-JAR a Web archive is used.

References:

http://www.hibernate.org/

http://www.hibernate.org/hib_docs/reference/en/html/quickstart.html#quickstart-intro

http://www.hibernate.org/hib_docs/api/

Topic ConfigureHibernate . { Edit | Ref-By | Printable | Diffs r2 < r1 | More }
 XML java.net RSS

  

Revision r2 - 2005-07-23 - 20:49:38 - boris_steiner