Hibernate in WSAD 5.x

Taken from a forum posting by hgrongstad - thanks for that.

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:

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

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.)


 NEW COMMENT

NoClassDefFoundError
03 Oct 2004, 15:19
aamir
I created a simple EJB project in WSAD 5.1.2 and imported followin 
files in EAR project:

cglib-full-2.0.1.jar
commons-collections-2.1.jar
commons-lang-1.0.1.jar
commons-logging-1.0.3.jar
dom4j-1.4.jar
hibernate2.jar
odmg-3.0.jar
xalan-2.4.0.jar

after that I added all jar file in the build path of EJB project and 
deploy the bean on test server.

When I try create session factory from my session bean it gives the 
following exception:

java.rmi.ServerException: RemoteException occurred in server thread; 
nested exception is: 
	java.rmi.RemoteException: ; nested exception is: 
	java.lang.NoClassDefFoundError: 
net/sf/hibernate/cfg/Configuration
	at com.ibm.CORBA.iiop.UtilDelegateImpl.mapSystemException
(UtilDelegateImpl.java:156)
	at com.ibm.CORBA.iiop.UtilDelegateImpl.wrapException
(UtilDelegateImpl.java:684)
	at javax.rmi.CORBA.Util.wrapException(Util.java:295)
	at dash.hr.services._EmployeeService_Stub.createEmployee
(_EmployeeService_Stub.java:255)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:79)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:41)
	at java.lang.reflect.Method.invoke(Method.java:386)
	at com.ibm.etools.utc.model.ReflectionMethodModel.invoke
(ReflectionMethodModel.java:68)
	at com.ibm.etools.utc.servlet.InvokeServlet.invoke
(InvokeServlet.java:110)

.............
 
where to put hibernate.cfg.xml
03 Oct 2004, 15:34
aamir
Where we have to put hibernate.cfg.xml, in EAR project or EJB project 
and in which folder ?
 
Re: where to put hibernate.cfg.xml
13 Oct 2004, 12:51
Tom Kucharski
On 03 Oct 2004 15:34, aamir wrote:

>Where we have to put hibernate.cfg.xml, in EAR project or EJB project
>and in which folder ?


You should add it to ejb project to the root directory of your 
classes. This is where your META-INF folder resides. 
Hibernate looks for hibernate.cfg.xml file using getResourceAsStream() 
method on class Configuration. 

That's why you should put hibernate2.jar into your ear project - it 
must be loaded by the same ClassLoader to work.
 
Re: NoClassDefFoundError
13 Oct 2004, 12:54
Tom Kucharski
Reply is in previous message's reply :-). There must be only one 
classloader that will load your ear and hibernate2.jar. 
That's why you should put hibernate.jar file into ear project directory
 
ad 3: Add the above jars to the project's java build path
21 Oct 2004, 11:56
marchart
The preffered way should *not* be to add the jar files to the 
EJB project's build path. Adding the jar files to the project's
classpath results in proper compiling but ClassDefNotFound 
Exceptions running the EA on the server due to the wrong 
runtime classpath. 


Instead use the WSAD JAR Dependency Editor (Right Click on your EJB 
Module --> Open with JAR Dependeny Editor) and add those jar files 
here. The EJB-Module's classpath and the MANIFEST.MF are properly
set and avoids adding all hibernate jar files to the 
Websphere's ws.ext.dirs

Best Regards
 
Could not obtain WebSphere JTSXA instance
04 May 2005, 04:40
saurabhnarang
This is the error I encounter when I try to rum hibernate with WSAD, 
please help...

java.lang.ExceptionInInitializerError: 
org.hibernate.HibernateException: Could not obtain WebSphere JTSXA 
instance
	at 
org.hibernate.transaction.WebSphereTransactionManagerLookup.getTransact
ionManager(WebSphereTransactionManagerLookup.java:49)
	at org.hibernate.impl.SessionFactoryImpl.<init>
(SessionFactoryImpl.java:264)
	at org.hibernate.cfg.Configuration.buildSessionFactory
(Configuration.java:1055)
	at org.classes.HibernateUtil.<clinit>(HibernateUtil.java:40)
	at org.classes.EventManager.main(EventManager.java:25)
Caused by: java.lang.ClassNotFoundException: com.ibm.ejs.jts.jta.JTSXA
	at java.net.URLClassLoader.findClass(URLClassLoader.java
(Compiled Code))
	at java.lang.ClassLoader.loadClass(ClassLoader.java(Compiled 
Code))
	at sun.misc.Launcher$AppClassLoader.loadClass
(Launcher.java:460)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:448)
	at java.lang.Class.forName1(Native Method)
	at java.lang.Class.forName(Class.java:142)
	at 
org.hibernate.transaction.WebSphereTransactionManagerLookup.getTransact
ionManager(WebSphereTransactionManagerLookup.java:40)
	... 4 more
Exception in thread "main"
 
Re: Could not obtain WebSphere JTSXA instance
23 Jun 2005, 09:58
shaarky
I had the same problem in obtaining WebSphere JTSXA . in WAS 5.1 , but
it sould work normally in WAS 5.0

Check the following link:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-198

I believe this problem have been solved in Hibernate 3 , even with WAS 6.

But i have not tried Hibernate 3 with WSAD 5.1.2 (WAS 5.1)yet , hence
the Hibernate Syncronizer version that support Hibernate 3 , does not
work in WSAD(Eclipse 2.1). and the generated classes reference the
net.sf.* packages.

Either you have to work in Eclipse 3(Rational Application Developer RAD 6).

Or find away to rewrite the hibernator templates 2.1 to reference
org.hibernate.* instead of net.sf.*

But this will be hard hence i believe that hibernator classes reference
the net.sf.* packages in the plugin jar file.

You can ommit the Transaction property in the Configuration file ,I
tired it and it worked, but this is not advisable in production
environment , hence u work in process level transaction , i.e  with out
transaction manager.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值