現在網上流傳的關于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'?> <hibernate-configuration> ??? <session-factory name="hibernate.session_factory"> ??????? <!-- Database connection settings --> ??????? <!-- JDBC connection pool (use the built-in) --> ??????? <!-- SQL dialect --> ??????? <!-- Enable Hibernate's automatic session context management --> ??????? <!-- Disable the second-level cache? --> ??????? <!-- Echo all executed SQL to stdout --> ??????? <!-- Drop and re-create the database schema on startup --> ??????? <mapping resource="com/hyq/src/common/UserVO.hbm.xml"/> ??? </session-factory> </hibernate-configuration> 注意:此處我使用的connection.datasource為:TestDS。這是因為我在weblogic中配置的數據源就是TestDS。這里要和你配置的數據源保持一致。 實現監聽接口的方法如下(通過此方法完成配置文件的加載,從而達到發布jndi的目的): import org.hibernate.SessionFactory; public class HibernateInit ? public void contextInitialized(ServletContextEvent servletContextEvent) { 實現ServletContextListener接口后要在web.xml中進行配置,如下: 獲取SessionFactory的方法如下: import org.hibernate.SessionFactory; ????? static { ????? public static SessionFactory getSessionFactory() { 具體完整的示例:HibernateJNDI源代碼
<!DOCTYPE hibernate-configuration PUBLIC
??????? "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
??????? "
??????? <!--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>
??????? <property name="connection.pool_size">1</property>
??????? <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
??????? <property name="current_session_context_class">thread</property>
??????? <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
??????? <property name="show_sql">true</property>
??????? <!--property name="hbm2ddl.auto">create</property-->
package com.hyq.src.servlets;
import org.hibernate.cfg.Configuration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
??? implements ServletContextListener {
? public void contextDestroyed(ServletContextEvent servletContextEvent) {
? }
??? try {
????? Configuration conf = new Configuration().configure();
????? SessionFactory sf = conf.buildSessionFactory();
??? }
??? catch (Exception e) {
????? e.printStackTrace();
??? }
? }
}
? <listener>
???? <listener-class>com.hyq.src.servlets.HibernateInit</listener-class>
? </listener>
注意:要加載在<servlet>之前。
package com.hyq.src.util;
import javax.naming.Context;
import javax.naming.InitialContext;
public class HibernateUtil {
? public HibernateUtil() {
? }
? private static final SessionFactory sessionFactory;
????????? 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);
????????? }
????? }
????????? 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();