首页 > 技术文章 > Hibernate学习笔记!

arriw 2016-06-28 09:39 原文

Hibernate 是一个开放源代码的对象关系映射框架,它对 JDBC 进行了轻量级的对象封装,使 Java 程序员可以随心所欲的使用对象编程思维来操纵数据库。它不仅提供了从 Java 类到数据表之间的映射,也提供了数据查询和恢复机制。相对于使用 JDBC 和 SQL 来手工操作数据库,Hibernate 可以大大减少操作数据库的工作量。 另外 Hibernate 可以利用代理模式来简化载入类的过程,这将大大减少利用 Hibernate QL 从数据库提取数据的代码的编写量,从而节约开发时间和开发成本 Hibernate 可以和多种Web 服务器或者应用服务器良好集成,如今已经支持几乎所有的流行的数据库服务器。

1、SessionFactory:

Configuration的实例会根据当前的配置信息,构造SessionFactory实例。SessionFactory是线程安全的,一般情况下一个应用中一个数据库共享一个SessionFactory实例。
构建SessionFactory Hibernate 的SessionFactory接口提供Session类的实例,Session类用于完成对数据库的操作。由于SessionFactory实例是线程安全的(而Session实例不是线程安全的) ,所以每个操作都可以共用同一个SessionFactory来获取Session。
从XML文件读取配置信息构建SessionFactory: (1)创建一个Configuration对象,并通过该对象的configura()方法加载Hibernate配置文件,代码如下。 Configuration config = new Configuration().configure();
configure() 方法:用于告诉Hibernate加载hibernate.cfg.xml文件。Configuration在实例化时默认加载classpath中的 hibernate.cfg.xml,当然也可以加载名称不是hibernate.cfg.xml的配置文件,例如 wghhibernate.cfg.xml,可以通过以下代码实现。 Configuration config = new Configuration().configure("wghhibernate.cfg.xml");

Java代码
  1. public class AppManager extends ContextLoaderListener implements 
  2.         ServletContextListener { 
  3.  
  4.     private ServletContext context; 
  5.     private WebApplicationContext webApplicationContext; 
  6.  
  7.     public void contextDestroyed(ServletContextEvent sce) { 
  8.         Enumeration<String> enumeration = this.context.getAttributeNames(); 
  9.         while (enumeration.hasMoreElements()) { 
  10.             context.removeAttribute(enumeration.nextElement()); 
  11.         } 
  12.     } 
  13.  
  14.     public void contextInitialized(ServletContextEvent sce) { 
  15.         this.context = sce.getServletContext(); 
  16.         this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context); 
  17.         this.context.setAttribute("WEBAPPLICATIONCONTEXT", webApplicationContext); 
  18.          
  19.         String path = this.context.getRealPath(this.context.getInitParameter("configUrl")); 
  20.         ConfigurationFactory factory = new ConfigurationFactory(path); 
  21.         Configuration config = factory.getConfiguration(); 
  22.         this.context.setAttribute(MyConstants.CONFIGURATION_KEY, config); 
  23.     } 

 

public class AppManager extends ContextLoaderListener implements
		ServletContextListener {

	private ServletContext context;
	private WebApplicationContext webApplicationContext;

	public void contextDestroyed(ServletContextEvent sce) {
		Enumeration<String> enumeration = this.context.getAttributeNames();
		while (enumeration.hasMoreElements()) {
			context.removeAttribute(enumeration.nextElement());
		}
	}

	public void contextInitialized(ServletContextEvent sce) {
		this.context = sce.getServletContext();
		this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		this.context.setAttribute("WEBAPPLICATIONCONTEXT", webApplicationContext);
		
		String path = this.context.getRealPath(this.context.getInitParameter("configUrl"));
		ConfigurationFactory factory = new ConfigurationFactory(path);
		Configuration config = factory.getConfiguration();
		this.context.setAttribute(MyConstants.CONFIGURATION_KEY, config);
	}
}

(2)完成配置文件和映射文件的加载后,将得到一个包括所有Hibernate运行期参数的Configuration实例,通过Configuration实例的buildSessionFactory()方法可以构建一个惟一的SessionFactory,代码如下。
SessionFactory sessionFactory = config.buildSessionFactory();
构建SessionFactory要放在静态代码块中,因为它只在该类被加载时执行一次。 一个典型的构建SessionFactory的代码如下。

public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {     try {              Configuration cfg = new Configuration().configure();               sessionFactory = cfg.buildSessionFactory();          } catch (Exception e) {               System.out.println(e.getMessage());         }        } }

 

推荐阅读