Hibernate with Weblogic(jndi)

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  

 

 

 

 

 

 

Hi Joy, 
I was also spending lot of time solving the issue with JNDI using Hibernate and Weblogic. And your solution provided me the answer. I did not copied any of the jar file to the domain lib folder. And I do not think that is required if those jars are inside your WEB-INF/lib folder. 
Also I did not changed the startWeblogic.cmd file to have those jars in the classpath as weblogic will pick it from WEB-INF/lib folder.

The only thing which I tried from your solution was the username and password. I was giving the oracle username and password, and I was getting User not Authorized error. Then i tried the weblogic server username and password and it worked. Here is my snipped from hibernate.cfg.xml :
<property name="show_sql">true</property>
<property name="connection.autocommit">true</property>
<property name="current_session_context_class">thread</property>
<property name="connection.datasource">FISDS</property>
<property name="connection.username">weblogic</property>
<property name="connection.password">weblogic</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="jndi.url">t3://127.0.0.1:7001</property>
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>

Anyway i got the solution from your post, although I did not have to do so many things as suggested by you. Thanks Again.

Prem Kashyap
SCJP 1.4

 

from:http://www.coderanch.com/t/415116/Object-Relational-Mapping/java/Hibernate-Weblogic-with-Oracle-Configuration

 

 

My own example:

 

<property name="show_sql">true</property>

<property name="connection.autocommit">true</property>

<property name="current_session_context_class">thread</property>

 

//in weblogic configuration, jdbc.moodle = jdbc.moodle

<property name="connection.datasource">jdbc.moodle</property>

 

//Use App Server Username/Pass not dbs

<property name="connection.username">admin</property>

<property name="connection.password">password</property>

 

<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

<property name="jndi.url">t3://127.0.0.1:7001</property>

<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>

 

 

 

 

Name Table sorted by this column in ascending orderJNDI NameTargets
moodle jdbc/moodle AdminServer
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章