zoukankan      html  css  js  c++  java
  • 第三十一天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(四)

                 6月19日,小雨。“黄梅时节家家雨。青草池塘处处蛙。有约不来过夜半,闲敲棋子落灯花。”

            面向对象无限包容的个性,给对SQL和数据库一窍不通的澳大利亚人Gavin King创造了极大的想象空间。那些原本尴尬的不利因素---OO对象模型和关系型数据库之间的设计理念上的差异即-“O/R阻抗失衡(O/R Impedance Mismatch)”等

    澳大利亚人的转化手段中,都被自觉或不自觉地消除了。

    Hibernate的出现,让面向对象的对象模型和关系型数据库的数据结构之间的相互转换达到了一种高峰。

    好的coder对Hibernate的Session的理解往往会让人大吃一惊。

    也许能够说,澳大利亚人用Session表达了自己柔软变通的适应性。

    全部的这些。让每个Session在open和close之间的短暂生命得到了升华。

                Session接口负责运行被持久化对象的CRUD操作(CRUD的任务是完毕与数据库的交流。包括了非常多常见的SQL语句。)。但须要注意的是Session对象是非线程安全的。

            1、自己动手。封装HibernateUtil类

         运用中为避免资源消耗,一般都会手动封装一个HibernateUtil类(未使用Spring管理的前提下)。该类的作用使Hibernate载入配置文件config, 创建sessionFactory等仅仅执行一次。

    package edu.eurasia.hibernate;
    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    
    /**
     * Configures and provides access to Hibernate sessions, tied to the current
     * thread of execution.
     */
    public class HibernateUtil {
    
    	/**
    	 * Location of hibernate.cfg.xml file. NOTICE: Location should be on the
    	 * classpath as Hibernate uses #resourceAsStream style lookup for its
    	 * configuration file. That is place the config file in a Java package - the
    	 * default location is the default Java package.<br>
    	 * <br>
    	 * Examples: <br>
    	 * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
    	 */
    	private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    
    	/** Holds a single instance of Session */
    	private static final ThreadLocal threadLocal = new ThreadLocal();
    
    	/** The single instance of hibernate configuration */
    	private static final Configuration cfg = new Configuration();
    
    	/** The single instance of hibernate SessionFactory */
    	private static org.hibernate.SessionFactory sessionFactory;
    
    	/**
    	 * Returns the ThreadLocal Session instance. Lazy initialize the
    	 * <code>SessionFactory</code> if needed.
    	 * 
    	 * @return Session
    	 * @throws HibernateException
    	 */
    	public static Session currentSession() throws HibernateException {
    		Session session = (Session) threadLocal.get();
    
    		if (session == null || !session.isOpen()) {
    			if (sessionFactory == null) {
    				try {
    					cfg.configure(CONFIG_FILE_LOCATION);
    					sessionFactory = cfg.buildSessionFactory();
    				} catch (Exception e) {
    					System.err.println("%%%% Error Creating SessionFactory %%%%");
    					e.printStackTrace();
    				}
    			}
    			session = (sessionFactory != null) ? sessionFactory.openSession()
    					: null;
    			threadLocal.set(session);
    		}
    
    		return session;
    	}
    
    	/**
    	 * Close the single hibernate session instance.
    	 * 
    	 * @throws HibernateException
    	 */
    	public static void closeSession() throws HibernateException {
    		Session session = (Session) threadLocal.get();
    		threadLocal.set(null);
    
    		if (session != null) {
    			session.close();
    		}
    	}
    
    	/**
    	 * Default constructor.
    	 */
    	private HibernateUtil() {
    	}
    }
    

             2、 Hibernate使用事务     

           Hibernate对JDBC进行了轻量级的封装,它本身在设计时并不具备事务处理功能。Hibernate将底层的JDBCTransaction或JTATransaction进行了封装,再在外面套上Transaction和Session的外壳,事实上是通过托付底层的JDBC或JTA来实现事务的处理功能的。      

    package edu.eurasia.hibernate;
    
    import java.util.Iterator;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    public class SimpleTransaction {
    	public static void main(String[] args) {
    		SimpleTransaction simpleTran = new SimpleTransaction();
    		simpleTran.tran();
    	}
    
    	// 演示事务使用的方法
    	public void tran() {
    		// 得到当前事务
    		Session session = HibernateUtil.currentSession();
    		// 声明事务
    		Transaction tx = null;
    		try {
    			// HQL查询语句
    			String hql = "from UserInfo";
    			// 事务的開始
    			tx = session.beginTransaction();
    			// 事务的中间操作
    			Iterator it = session.createQuery(hql).list().iterator();
    			while (it.hasNext()) {
    				UserInfo userinfo = (UserInfo) it.next();
    				System.out.println(userinfo.getUsername()+"  "+userinfo.getPassword());
    			}
    			// 提交事务
    			tx.commit();
    		} catch (Exception ex) {
    			if (tx != null) {
    				try {
    					// 回滚事务
    					tx.rollback();
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		} finally {
    			// 关闭session
    			session.close();
    		}
    	}
    }
    

               3、MySQL数据库UserInfo类,映射文件UserInfo.hbm.xml和hibernate.cfg.xml配置文件见:

    第十九天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(一) 

           4、还须要加入一个slf4j-nop-1.6.2.jar文件。

    project结构例如以下:

         际运用中,常常须要将当前线程和session绑定.一般的使用方法为使用ThreadLocal: 在HibernateUtil类中封装hibernate的管理.通过openSession取得session,并将其放入ThreadLocal变量中. 这样业务逻辑中仅需通过工具类取得当前线程相应的session.使用完成后,调用工具类closeSession方法将session关闭,当前线程的ThreadLocal变量置为NULL. 保证线程归还线程池复用后,ThreadLocal为空,以免出现导致其他线程訪问到本线程变量。

           后,Hibernate的SessionFactory提供获取session的新方法getCurrentSession (获得与当前线程绑定的session). 内部通过代理封装,此方式得到的session不仅和当前线程绑定,也无需手动开关. 默认在事务提交之后,session自己主动关闭。


               了,引入Spring之后.sessionfactory的创建等都交给spring管理.Spring也提供了HibernateTemplate。 HibernateDaoSupport这种封装方法。

    用户能够不再考虑session的管理,事务的开启关闭.仅仅需配置事务就可以。


  • 相关阅读:
    神奇的flex布局
    reset、revert、rebase
    Vue.filter过滤器
    moment.js时间格式化总结
    Vue之组件大全
    过滤器filter
    Vue之animate
    Vue之axios
    Mac OS系统上测试PHP代码前的准备工作 | 使用XAMPP搭建Apache服务器的步骤
    Python中的标识符、关键字、变量、语句、注释、模块
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5069103.html
Copyright © 2011-2022 走看看