现在网上流传的关于Weblogic上配置Hibernate为JNDI的方法多是robbin写的
http://forum.javaeye.com/viewtopic.php?t=245。google了好久,发现都大同小异,几乎都是一个模版,没办法,天下文章本就是一大抄,今天你抄我,明天我抄你。我是一个很懒的人,一看到那么复杂的配置(主要是Weblogic启动脚本的修改),头就大了,也没有试下去的勇气了,以前倒好说,毕竟没有用到容器托管,如今用到了,自然也不能置之不理。就找点资料,自己测试,发现可以通过实现ServletContextListener接口来加载配置文件,从而达到自己的需求。以下是源代码和配置文件:
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 name="hibernate.session_factory">
<!-- Database connection settings -->
<!--property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:hyq</property>
<property name="connection.username">hyq</property>
<property name="connection.password">hyq</property-->
<property name="connection.datasource">TestDS</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--property name="hbm2ddl.auto">create</property-->
<mapping resource="com/hyq/src/common/UserVO.hbm.xml"/>
</session-factory>
</hibernate-configuration>
注意:此处我使用的connection.datasource为:TestDS。这是因为我在weblogic中配置的数据源就是TestDS。这里要和你配置的数据源保持一致。
实现监听接口的方法如下(通过此方法完成配置文件的加载,从而达到发布jndi的目的):
package com.hyq.src.servlets;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class HibernateInit
implements ServletContextListener {
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
public void contextInitialized(ServletContextEvent servletContextEvent) {
try {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
实现ServletContextListener接口后要在web.xml中进行配置,如下:
<listener>
<listener-class>com.hyq.src.servlets.HibernateInit</listener-class>
</listener>
注意:要加载在<servlet>之前。
获取SessionFactory的方法如下:
package com.hyq.src.util;
import org.hibernate.SessionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
public class HibernateUtil {
public HibernateUtil() {
}
private static final SessionFactory sessionFactory;
static {
try {
Context ctx = new InitialContext();
sessionFactory = (SessionFactory) ctx.lookup("hibernate/session_factory");
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
然后就可以在其他方法中使用SessionFactory 了,如:
Session session = HibernateUtil.getSessionFactory()
.getCurrentSession();
session.beginTransaction();
request.setAttribute("message","已经成功运行!");
UserVO userVO = new UserVO();
userVO.setUser_name("TrampEagle");
session.save(userVO);
session.getTransaction().commit();
具体完整的示例:HibernateJNDI源代码