决定系统的整理下HIbernate的各方面知识。先重新搭建一次纯Hibernate的架子。
1.找齐hibernate需要的各种jar包,oracle的jdbc驱动,junit测试,hibernate需要的日志包(hibernate内置slf4j的接口,但是没有实现的api,自己找一个,slf4j-simple,或者用log4j,懒得麻烦直接用slf4-simple了)工程用maven构建,在此贴出POM文件。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.yyf</groupId>
<artifactId>hibernateexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<hibernate.version>3.6.9.Final</hibernate.version>
<hibernate-ehcache.version>3.6.1.Final</hibernate-ehcache.version>
<junit.version>4.10</junit.version>
</properties>
<dependencies>
<!--Oracle-JDBC 版权问题中央仓库没有,自己存入本地仓库-->
<dependency>
<groupId>org.yyf</groupId>
<artifactId>ojdbc6</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- junit 范围为test,这样打包的时候就不会打进去 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate-ehcache.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.16.1-GA</version>
</dependency>
</dependencies>
</project>
2.hibernate基本配置文件hibernate.cfg.xml,用于配置SessionFactory相关配置。
<?xml version='1.0' encoding='utf-8'?>
<!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3软件包中的src\org\hibernate目录中找到此文件 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--声明Hibernate配置文件的开始 -->
<hibernate-configuration>
<!--表明以下的配置是针对session-factory配置的,SessionFactory是Hibernate中的一个类,这个类主要负责保存HIbernate的配置信息,以及对Session的操作 -->
<session-factory>
<!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
<property name="hibernate.connection.url">
jdbc:oracle:thin:@localhost:1521:xe
</property>
<!--连接数据库是用户名 -->
<property name="hibernate.connection.username">test </property>
<!--连接数据库是密码 -->
<property name="hibernate.connection.password">test </property>
<!--数据库连接池的大小 -->
<property name="hibernate.connection.pool.size">20 </property>
<!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率 -->
<property name="hibernate.show_sql">true </property>
<!--jdbc.fetch_size是指Hibernate每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch
Size越小,读数据库的次数越多,速度越慢 -->
<property name="jdbc.fetch_size">50 </property>
<!--jdbc.batch_size是指Hibernate批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大 -->
<property name="jdbc.batch_size">23 </property>
<!--jdbc.use_scrollable_resultset是否允许Hibernate用JDBC的可滚动的结果集。对分页的结果集。对分页时的设置非常有帮助 -->
<property name="jdbc.use_scrollable_resultset">false </property>
<!--connection.useUnicode连接数据库时是否使用Unicode编码 -->
<property name="Connection.useUnicode">true </property>
<!--connection.characterEncoding连接数据库时数据的传输字符集编码方式,最好设置为gbk,用gb2312有的字符不全 -->
<property name="connection.characterEncoding">utf-8 </property>
<!-- 每次启动hibernate时自动 更新/创建 表结构 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。 -->
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect </property>
<!-- ORM配置用xml的时候用resource,注释用class -->
<!-- <mapping resource="com/octopus/hibernate/model/Student.hbm.xml"/>-->
<mapping class="org.yyf.hibernateexample.domain.onetoone.twoway.Husband" />
<mapping class="org.yyf.hibernateexample.domain.onetoone.twoway.Wife" />
</session-factory>
</hibernate-configuration>
3.在没有Spring的环境下,我们如果使用SessionFactory,应为其创建一个单例模式使用,SessionFactory是线程安全的,并且创建很耗费资源。一个数据源只需要一个。
这里顺便复习下以前写的单例模式的各种创建方式。
package org.yyf.hibernateexample.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 辅助类,延迟加载单例SessionFacory
* @author yeyf
* @date 2013年12月12日
*/
public class HibernateUtil {
private static class SessionFactoryContainer {
private static final SessionFactory sessionFactroy = new Configuration()
.configure("hibernate.cfg.xml").buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return SessionFactoryContainer.sessionFactroy;
}
public static Session getSession(){
return getSessionFactory().openSession();
}
private HibernateUtil() {
}
}
So easy