Hi,
Today after 20 hrs of continous struggling I was able configure my existing Enterprise application
in Weblogic 8.1 sp4 with Hibernate 3.
I have seen blogs about this in the beadev2dev that you have to create a WLS Startup class to bind the Session Factory and other things but Weblogic 8.1 works fine without any Start Up Classes
Important Things to note
1) Libraries you have to use in your startWeblogic.cmd CLASSPATH
Copy this libraries in your domains/mydomain/lib folder
1)
These are the important libraries
log4j.jar
hibernate3.jar;
providerutil.jar;
dom4j-1.6.1.jar;
commons-collections-2.1.1.jar;
jta.jar;
asm.jar;
asm-attrs.jar;
commons-logging.jar;
antlr-2.7.5H3.jar;
antlr-2.7.6rc1.jar;
ant-1.6.5.jar;
cglib-2.1.3.jar //Very very important (took my most time to find this issue
you will get NoClassDefFoundError
2)
My startWeblogic.cmd relevant portion is like this
JOY_DOM is my domain name
Path: C:\bea\user_projects\domains\joydom
JOY_DOM=C:\bea\user_projects\domains\joydom
set CLASSPATH=%WEBLOGIC_CLASSPATH%;%POINTBASE_CLASSPATH%;%JAVA_HOME%\jre\lib\rt.jar;%WL_HOME%\server\lib\webservices.jar;%JOY_DOM%\lib\commons-logging.jar;%JOY_DOM%\lib\log4j.jar;%JOY_DOM%\lib\log4j_appserver.jar;%JOY_DOM%\lib\jdom.jar;%JOY_DOM%\lib\xmlrpc-1.1.jar;%JOY_DOM%\lib\hibernate3.jar;%JOY_DOM%\lib\providerutil.jar;%JOY_DOM%\lib\dom4j-1.6.1.jar;%JOY_DOM%\lib\commons-collections-2.1.1.jar;%JOY_DOM%\lib\jta.jar;%JOY_DOM%\lib\asm.jar;%JOY_DOM%\lib\asm-attrs.jar;%JOY_DOM%\lib\commons-logging.jar ;%JOY_DOM%\lib\antlr-2.7.5H3.jar;%JOY_DOM%\lib\antlr-2.7.6rc1.jar;%JOY_DOM%\lib\ant-1.6.5.jar;%JOY_DOM%\lib\cglib-2.1.3.jar;%CLASSPATH%
3) Configure a datasource using a connectioon pool
4) Now the most important part hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">weblogic</property> //Use App Server Username/Pass not dbs (other wise UserNotFound SQLException)
<property name="connection.password">weblogic</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.datasource">jdbc/JOYSDataSource</property> //configure this datasource in Weblogic Admin Console
//You know to do this right other wise pls feel free to ask
<property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="transaction.factory.class">org.hibernate.transaction.JTATransactionFactory</property >
<property name ="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<!--property name="jndi.url">
t3://127.0.0.1:7050</property --> //jndi URL not required Weblogic knows it
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">jta</property>
<mapping resource="Event.hbm.xml"/>
</session-factory>
</hibernate-configuration >
Keep this in any folder yu like yu can keep it at domains/mydomain
I will tell how to right the code when constructing Configuration object with the file path in by code
5) Event.hbm.xml (You can keep it in the same folder as hibernate-cfg.xml or use Relative path (How it suits yu
Simple file one table
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="none" default-access="property" default-lazy="true" auto-import="true">
<class name="com.hibernate.joy.valueobj.EventInfo" table="EVENTS"> //Give fully classified name
<id name="eventId" column="EVENT_ID">
<generator class="native" />
</id>
<property name="eventDate" type="timestamp" column="EVENT_DATE" />
<property name="title" />
</class>
<sql-query name="select_title">
select title from events e where e.event_id = :id
</sql-query>
</hibernate-mapping>
6) Sample function for my
EJB My EJB is Container Managed
public
String getEventInfo(int eventId) throws GenEException {
EventInfo info = new EventInfo();
List eventList = new ArrayList();
String name = "Default";
try {
System.out.println("getEventInfo");
File file = new File("C:\\bea\\user_projects\\domains\\joydom\\hibernate-cfg.xml"); //yu see I have used full path
//This is my first run I will chenge to relative path later
System.out.println(file.exists());
SessionFactory sessionFactory = new Configuration().configure(file).buildSessionFactory();
if(sessionFactory == null) {
System.out.println("NULL"); //Testing it
}
else {
System.out.println("NOT NULL");
}
Session session = sessionFactory.getCurrentSession();
//session.beginTransaction(); //Not Required as Transaction is Container Managed hence commented
System.out.println("#######NAME####### " + name);
SQLQuery sql = (SQLQuery)session.getNamedQuery("select_title").setInteger("id",4);
eventList = ((Query)sql).list();
name = (String)(eventList.get(0));
System.out.println("#######NAME####### " + name);
//session.getTransaction().commit(); //Not Required as Transaction is Container Managed hence commented
return name;
}
catch(Exception ex){
ex.printStackTrace();
}
return name;
}
Hope this helps you guys and doesnt have to spend 20hrs googling till the whole Universe from Earth to Saturn
Pls feel free to ask any questions