This document outlines the steps for running Hibernate with an EJB application in the WSAD 5.x environment, using the
hibernate.cfg.xml flavor for the Configuration. This example assumes an Oracle9 database, but any flavor can, off course, be substituted.
The set-up of the DataSource in step 7 and 8 refers to settings for the Server configuration (test server) within WSAD.
1. Create the EAR and EJB applications as you normally do.
2. Add the folowing jar archives to the EAR by importing them from the file system:
cglib2.jar
commons-collections.jar
commons-logging.jar
dom4j.jar
hibernate2.jar
odmg.jar
xalan.jar
3. Add the above jars to the project's java build path
4. Place the jar file
commons-lang.jar in the server's ext library (Don't know why this is, but it's
the only way it will work. Maybee a subsequent version of hibernate will fix this).
5. Add
commons-lang.jar to the project's java build path.
6. The supporting files for hibernate are now in place.
7. Configure a DataSource the normal way. (I also set up a JAASC Authentication entry under the Security tab and then set the Container managed Authentication Alias for the DataSource to that )
8. Create a resource reference to the DataSource from the EJB Deployment descriptor:
name: jdbc/data_source_name
type: javax.sql.DataSource
authentication: Container
Sharing Scope: Shareable
jndi name: jdbc/data_source_name
Isolation level: TRANSACTION_READ_COMMITTED
Connection Policy: Default
8. Create the class.hbm.xml files and hibernate.cfg.xml and place them at the EJB module level.
9. An example hibernate.cfg.xml that makes use of the DataSource set up in step 7:
Code:
<?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>
<!-- Data Source -->
<property name="connection.datasource">java:comp/env/jdbc/data_source_name</property>
<!-- Database Settings -->
<property name="default_schema">SCHEMA_NAME</property>
<property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<!-- JDBC Settings -->
<property name="jdbc.use_streams_for_binary">true</property>
<property name="max_fetch_depth">1</property>
<!-- Cache settings -->
<property name="cache.provider_class">net.sf.hibernate.cache.HashtableCacheProvider</property>
<!-- Transaction API -->
<property name="transaction.manager_lookup_class">
net.sf.hibernate.transaction.WebSphereTransactionManagerLookup</property>
<!-- Mapping files -->
<mapping resource="Event.hbm.xml"/>
<mapping resource="Attendance.hbm.xml"/>
<mapping resource="Offering.hbm.xml"/>
<mapping resource="OfferingAssistance.hbm.xml"/>
<mapping resource="OfferingStatusCode.hbm.xml"/>
<mapping resource="Site.hbm.xml"/>
<mapping resource="SubjectArea.hbm.xml"/>
</session-factory>
</hibernate-configuration>
You should now be able to code a simple client inside a SessionBean that accesses your data. My (rough) code goes something like this:
Code:
SessionFactory sessions = null;
try {
sessions = new Configuration().configure().buildSessionFactory();
//sessions = cfg.buildSessionFactory();
} catch (HibernateException e) {
print(" Exception creating sessionfactory: " + e.getMessage());
}
Session session = null;
try {
session = sessions.openSession();
BigDecimal num = new BigDecimal("12");
SubjectArea holder = (SubjectArea) session.load(SubjectArea.class, num);
System.out.println("-->Loaded SubjectArea object (PARENT) ");
System.out.println("------------------------------");
System.out.println("-->subjectAreaID = " + holder.getSbjAreaName());
System.out.println("-->Subject area void indicator = " + holder.getSbjAreaVoidInd());
System.out.println("-->Got " + holder.getEvents().size() + " Event objects");
System.out.println("-------------------------------\n");
.........
.........
Hope this helps those of you out there new to Hibernate in the WSAD environment. Please add to this. (I'll try to add the deployment part onto WAS once I understand it.)