论坛首页 Java企业应用论坛

经过2天的折腾,和这里朋友的帮助,完成了用struts建立Sessi...

浏览 28533 次
该帖已经被评为精华帖
作者 正文
   发表时间:2003-09-18  
我用的是jbuilder9调试的

1、建立一个Class,HibernatePlugIn 放在src下
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

public class HibernatePlugIn implements PlugIn {
    public void destroy(); {
    }

    public void init(ActionServlet servlet, ModuleConfig config); throws ServletException {
      try{
        ServletContext context = null;
        context = servlet.getServletContext();;
        SessionFactory sf = new Configuration();.configure();.
            buildSessionFactory();;
        context.setAttribute("net.sf.hibernate.SessionFactory", sf);;
      }
      catch(Exception e);{
        e.printStackTrace();;
      }
    }
}


2、在struts-config.xml中添加
<plug-in className="HibernatePlugIn">
  <set-property property="configFilePath" value="/hibernate.cfg.xml" />
  <set-property property="storeInServletContext" value="true" />
</plug-in>


3、将hibernate.cfx.xml放在src目录下,并且属性设为copy
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>
        <property name="show_sql">false</property>
        <property name="use_outer_join">true</property>
        <property name="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.100.242:1521:ecm</property>
        <property name="hibernate.connection.username">mydb_user</property>
        <property name="hibernate.connection.password">mydb_pass</property>
        <property name="hibernate.connection.pool.size">20</property>
	<property name="session_factory_name">hibernate/session_factory</property>
        <!-- Mapping files -->
        <mapping resource="Content_folder.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


这样以后就可以在任何地方调用了
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import javax.naming.InitialContext;
import javax.naming.Context;


public class MyService {
  private SessionFactory sf;

  public LoginService(); throws Exception {
    Context ctx = new InitialContext();;
    sf = (SessionFactory); ctx.lookup("hibernate/session_factory");;
  }
}


我经验少,是个hibernate的菜鸟,有不对的地方,大家帮忙指出来!
   发表时间:2003-09-18  
当时我用tomcat,想把SessionFactory绑定在jndi上,没有成功,谁有详细方法,拜托贴出来
0 请登录后投票
   发表时间:2003-09-18  
Jimmy 写道
不错啊,我原来是写一个servlet代码做Init SessionFactory这件事情,再将这个servlet设置成Load-on-startup。

我开始也是哪样作的,可是只有在servlet才能得到sessionFactory,普通的class得不到,郁闷啊
0 请登录后投票
   发表时间:2003-09-18  
zhenglinxi 写道
Jimmy 写道
不错啊,我原来是写一个servlet代码做Init SessionFactory这件事情,再将这个servlet设置成Load-on-startup。

我开始也是哪样作的,可是只有在servlet才能得到sessionFactory,普通的class得不到,郁闷啊

我前几天使用struts+hibernate的做法:
得到SessionFactory一样是用的Struts plugin
所有持久操作都放在DAO中
实现一个signlton 的 Abstract DAOFactory
Abstract DAOFactory一个init方法
public abstract void init(Object theObj);;
继承struts的ActionServlet,改写init方法
	public void init(); throws ServletException {
		super.init();;
		ServletContext context = getServletContext();;
		//init hibernate

		Object sessionFactory =
			context.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY);;

		// init the factory
		DAOFactory factory = DAOFactory.getInstance();;
		factory.init(sessionFactory);;

                                ...
	}

这里将sessionFactory取出初始化DAOFactory

实现一个concrete DAOFactory,实现init方法保存sessionFactory
public void init(Object theObj); {
		if (theObj instanceof SessionFactory); {
			sessionFactory = (SessionFactory); theObj;
		}

提供一个getSession方法
public static Session getSession(); throws HibernateException {
		if (sessionFactory == null); {
			throw new HibernateException("SessionFactory has not been initialized");;
		}
		return sessionFactory.openSession();;
	}

以后DAO中所有需要Session的地方调用下面代码就可以就可以
ConcreteDAOFactory.getSession();;
0 请登录后投票
   发表时间:2003-09-19  
能不能给个连贯的例子,这样东一句本一句的很难读呀!

比如说楼主的贴子,在最后是用JNDI查询SessionFactory的,但在什么地上注册就个JNDI的,而前面在说明时factory是保存在ServletContext中的,又是怎么回事?

本人想东西比较慢,还请各位写清楚一些,谢了!
0 请登录后投票
   发表时间:2003-09-19  
JNDI是由struts注册的
0 请登录后投票
   发表时间:2003-09-19  
能介绍一下struts怎样绑定哪个SessionFactory的吗?
0 请登录后投票
   发表时间:2003-09-20  
Struts怎么对JNDI进行注册?
0 请登录后投票
   发表时间:2003-09-25  
zhenglinxi 写道
JNDI是由struts注册的

这里的JNDI不是由struts注册的
是struts通过pluin时new Configuration().configure().configuration.buildSessionFactory();
这时因为hibernate读取到hibernate.cfg.xml
由于存在
<property name="session_factory_name">hibernate/session_factory</property> 

所以它用net.sf.hibernate.util.NamingHelper的public static void bind(Context ctx, String name, Object val) throws NamingException 来注册JNDI;大家可以打开Configuration.java的源码仔细往下搜索就可以发现了。
0 请登录后投票
   发表时间:2003-09-26  
xmvigour 写道
zhenglinxi 写道
JNDI是由struts注册的

这里的JNDI不是由struts注册的
是struts通过pluin时new Configuration().configure().configuration.buildSessionFactory();
这时因为hibernate读取到hibernate.cfg.xml
由于存在
<property name="session_factory_name">hibernate/session_factory</property> 

所以它用net.sf.hibernate.util.NamingHelper的public static void bind(Context ctx, String name, Object val) throws NamingException 来注册JNDI;大家可以打开Configuration.java的源码仔细往下搜索就可以发现了。


高手
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics