Weblogic Server 8.1 and Hibernate work very nicely together. Set up your datasource in Weblogic and for the example hibernate config as shown below, configure a data source called 'my.data.source (for example)'.
This example is specifcally geared towards struts users wishing to bind their sesion factory to JNDI with Weblogic Server.
Hibernate.cfg.xml
Code:
<session-factory name="my.factory.name">
<!-- Bind the SesssionFactory to JNDI -->
<!-- JNDI DataSource Connection -->
<property name="connection.datasource">my.data.source</property>
<!-- Transaction Management Properties-->
<property name="transaction.manager_lookup_class">
net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
</property>
<property name="transaction.factory_class">
net.sf.hibernate.transaction.JTATransactionFactory
</property>
<!-- Miscellaneous Properties -->
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">true</property>
<!-- mapping files -->
<mapping resource="a.hbm.xml"/>
<mapping resource="b.hbm.xml"/>
<mapping resource="c.hbm.xml"/>
</session-factory>
An adaption of the HibernatePlugin.java found from wiki 105:
Code:
import javax.servlet.ServletException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
public class HibernatePlugin implements PlugIn
{
private static Log log = LogFactory.getLog(HibernatePlugin.class);
private static SessionFactory sf = null;
public void destroy()
{
try
{
log.info("Closing SessionFactory");
sf.close();
log.info("SessionFactory successfully closed");
}catch(Throwable t)
{
log.error("Unbind SesssionFactory failed",t);
}
}
public void init(ActionServlet servlet, ModuleConfig config) throws ServletException
{
try{
log.info("Initiating bind SessionFactory to JNDI");
sf = new Configuration().configure().buildSessionFactory();
log.info("SessionFactory successfully bound to JNDI");
}catch(Throwable t)
{
log.fatal("Binding SessionFactory to JNDI failed",t);
throw new ServletException(t);
}
}
}
Configure the above class as a plugin in your struts xml or simply use it as the basis for an init type of action. Next thing you know, you're ready to roll with Hibernate and Weblogic server talking to eachother nicely.
To pull your session factory from JNDI you can use the following:
Code:
Context ctx = new InitialContext();
SessionFactory sessionFactory = (SessionFactory) ctx.lookup("hibernate.HibernateFactory");
That will at least get you a good push-start.
Good luck,
Joe