首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

上面是JSP项目中,hibernate技术中的一个代码,帮忙说说这个代码是做什么用的

2012-12-31 
下面是JSP项目中,hibernate技术中的一个代码,帮忙说说这个代码是做什么用的package cn.myexam.hibernatei

下面是JSP项目中,hibernate技术中的一个代码,帮忙说说这个代码是做什么用的
package cn.myexam.hibernate;

import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import javax.servlet.ServletException; 

import org.hibernate.HibernateException;

import org.apache.struts.action.ActionServlet; 
import org.apache.struts.action.PlugIn; 
import org.apache.struts.config.ModuleConfig; 


public class InitHibernate implements PlugIn { 

private Context ctx; 

public void destroy() {

if (ctx != null) { 
try { 

ctx.unbind("HibernateSessionFactory"); 
} catch (NamingException e) { 
e.printStackTrace(); 


if (HibernateSessionFactory.getSessionFactory() != null) { 
try { 

HibernateSessionFactory.getSessionFactory().close(); 
} catch (HibernateException e) { 
e.printStackTrace(); 






public void init(ActionServlet servlet, ModuleConfig config) 
throws ServletException { 
try { 

HibernateSessionFactory.rebuildSessionFactory();

if(HibernateSessionFactory.getSessionFactory()!=null)
System.out.println("SessionFactory has be successfully builded!");
} catch (HibernateException ex) { 
throw new RuntimeException( 
"Exception building SessionFactory: " + ex.getMessage(), 
ex); 


try { 

ctx = new InitialContext(); 

ctx.bind("HibernateSessionFactory", HibernateSessionFactory.getSessionFactory()); 
} catch (NamingException ex) { 
throw new RuntimeException( 
"Exception binding SessionFactory to JNDI: " + ex.getMessage(), 
ex); 


帮忙详细说说每个函数是做什么用的
[解决办法]
这个应该是在写一个过滤器,但是具体哪个方法做什么我也不是很清楚。你可以看一下过滤器相关的知识
[解决办法]
初始化啊,destory()销毁当前的HibernateSessionFactory
init()重新实例化一个SessionFactory绑定到jndi上以供使用。
[解决办法]
hibernate链接数据库的工程类
[解决办法]

public void destroy() {//这个方法销毁HibernateSessionFactory

if (ctx != null) { 
try { 
ctx.unbind("HibernateSessionFactory"); //从上下文中解除原来的HibernateSessionFactory如果存在的话
} catch (NamingException e) { 
e.printStackTrace(); 


if (HibernateSessionFactory.getSessionFactory() != null) { 
try { 

HibernateSessionFactory.getSessionFactory().close(); //关闭原来的HibernateSessionFactory如果存在的话
} catch (HibernateException e) { 
e.printStackTrace(); 





[解决办法]
public void init(ActionServlet servlet, ModuleConfig config) //这个方法重新绑定一个HibernateSessionFactory到上下文中
throws ServletException { 


try { 

HibernateSessionFactory.rebuildSessionFactory();//重建

if(HibernateSessionFactory.getSessionFactory()!=null)
System.out.println("SessionFactory has be successfully builded!");
} catch (HibernateException ex) { 
throw new RuntimeException( 
"Exception building SessionFactory: " + ex.getMessage(), 
ex); 


try { 

ctx = new InitialContext(); 

ctx.bind("HibernateSessionFactory", HibernateSessionFactory.getSessionFactory()); //绑定前面经过重建的HibernateSessionFactory到上下文中
} catch (NamingException ex) { 
throw new RuntimeException( 
"Exception binding SessionFactory to JNDI: " + ex.getMessage(), 
ex); 

热点排行