Hibernate3.1的简单使用
Hibernate的优点
关于Hibernate,我们首先要了解的是我们为什么要用他,他有什么优点。这些问题也可以归类成一个问题,哪就是他与JSBC有什么差别?
相同点:
两者都是JAVA的数据库操作中间件;
两者对于数据库进行直接操作的对象都不是线程安全的,都需要及时关闭;
两者都可以对数据库的更新操作进行显式的事务处理。
不同点:
使用的SQL语言不同:JDBC使用的是基于关系型数据库的标准SQL语言,Hibernate使用的是HQL(Hibernate query language)语言;
操作的对象不同:JDBC操作的是数据,将数据通过SQL语句直接传送到数据库中执行,Hibernate操作的是持久化对象,由底层持久化对象的数据更新到数据库中;
数据状态不同:JDBC操作的数据是“瞬时”的,变量的值无法与数据库中的值保持一致,而Hibernate操作的数据是可持久的,即持久化对象的数据属性的值是可以跟数据库中的值保持一致的。
注:实际上,不管CMP,Hibernate,JDO等等,所有的ORM都是对JDBC的封装。所以,如果是“完美精细”的JDBC绝对是性能最好的。
下面就开始我们的应用Hibernate之旅吧。
步骤一
要使用hibernate的前提当然是需要Hibernate的相关包,本文下载的是hibernate3.1,内部核心包是hibernate3.jar。把包导入到项目中。
步骤二
创建实体对象,就是写一个实体对象,创建Entity.java这里需要注意的是,该类需要提供所属各个属性的get/set方法。
步骤三
在实体类同目录下对实体类创建映射文件Entity.hbm.xml,是否同目录不是必须的,只是寻求方便寻找等。映射文件包含了实体对象的各个属性和其他实体对象关联关系等信息,例如:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.historycreator.hibernate"><class name="Entity" table="entity"><id name="id" column="ID"><generator /></id><property name="name" column="NAME" /><property name="area" column="AREA" /></class></hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration><session-factory><!-- Database connection settings --><property name="connection.driver_class">org.gjt.mm.mysql.Driver</property><property name="connection.url">jdbc:mysql://localhost:9090?useUnicode=true&characterEncoding=gbk</property><property name="connection.username">root</property><property name="connection.password">test</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">1</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- Enable Hibernate's automatic session context management --><property name="current_session_context_class">thread</property><!-- Disable the second-level cache --><property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><!-- Echo all executed SQL to stdout --><property name="show_sql">true</property><!-- Drop and re-create the database schema on startup update|create--><property name="hbm2ddl.auto">create</property><!-- 实体类的映射文件 --><mapping resource="entity/Entity.hbm.xml" /></session-factory></hibernate-configuration>
package util;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/** * * @author WizardAlick * Aug 11, 2010 */public class HibernateSessionFactory {private static SessionFactory sessionFactory;private static Configuration configuration = new Configuration();private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();private static String CONFIG_FILE_LOCATION = "/config/hibernate.cfg.xml";static{try {configuration.configure(CONFIG_FILE_LOCATION);sessionFactory = configuration.buildSessionFactory();} catch (HibernateException e) {System.err.println("create session factory is error !!");e.printStackTrace();}}public HibernateSessionFactory() {}/** * to get session * @return */public static Session getSession(){Session session = threadLocal.get();if (null == session || !session.isOpen()){if (null == sessionFactory){rebuildSessionFactory();}if (null == sessionFactory){return null;}session = sessionFactory.openSession();threadLocal.set(session);}return session;}/** * re create session factory */public static void rebuildSessionFactory(){try {configuration.configure(CONFIG_FILE_LOCATION);sessionFactory = configuration.buildSessionFactory();} catch (HibernateException e) {System.err.println("create session factory is error !!");e.printStackTrace();}}/** * close session */public static void closeSession(){Session session = threadLocal.get();threadLocal.set(null);if (null != session){session.close();}}/** * to get session factory */public static SessionFactory getSessionFactory(){return sessionFactory;}/** * to get configuration * @return */public static Configuration getConfiguration() {return configuration;}}