1、hibernate.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="包名">
<class name="类名" table="表名" >
<id name="主键在java类中的字段名" column="对应表中字段"type="类型 ">
<generator class="主键生成策略"/>
</id> -->
<class name="xu.hibernate.helloworld.News" table="NEWS">
<id name="id" column="ID" type="java.lang.Integer">
<!-- 指定主键的生成方式 ,native:使用数据库本地方式-->
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE"></column>
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR"></column>
</property>
<property name="date" type="java.util.Date">
<column name="DATE"></column>
</class>
</hibernate-mapping>
2、hibernate.cfg.xml 的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置连接数据库的基本信息 -->
<!-- DB连接四要素 -->
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DataBaseName=hibernate_1</property>
<property name="hibernate.connection.username">xu</property>
<property name="hibernate.connection.password">111111</property>
<!-- 配置连接数据库的基本信息 -->
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<!--C3P0 数据源(数据库连接池)-->
<!-- <property name="c3p0.min_size">5</property> <!—在连接池中可用数据库连接的最小数目
<property name="c3p0.max_size">30</property> 在连接池中所有数据库连接的最大数目
<property name="c3p0.time_out">1800</property> <!—设定数据库连接的超时时间
<property name="c3p0.max_statement">50</property> <!—可以被缓存的PreparedStatement的最大数目 -->
<!-- 当前Session上下文
<property name="hibernate.current_session_context_class">thread</property>
-->
<!-- 指定程序运行时是否在数据库自动生成数据表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 指定程序运行时是否在控制台输出SQL语句-->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 指定关联的.hbm.xml文件 -->
<mapping resource="xu/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、Session核心方法
对象的状态转换图
4. 使用 Hibernate 二级缓存的步骤:
1). 加入二级缓存插件的 jar 包及配置文件:
I. 复制 \hibernate-release-4.2.4.Final\lib\optional\ehcache*.jar 到当前 Hibrenate 应用的类路径下.
II. 复制 hibernate-release-4.2.4.Final\project\etc\ehcachexml 到当前 WEB 应用的类路径下
2). 配置 hibernate.cfg.xml
I. 配置启用 hibernate 的二级缓存
<property name="cache.use_second_level_cache">true</property>
II. 配置hibernate二级缓存使用的产品
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
III. 配置对哪些类使用 hibernate 的二级缓存
<class-cache usage="read-write" class="com.atguigu.hibernate.entities.Employee"/>
实际上也可以在 .hbm.xml 文件中配置对哪些类使用二级缓存, 及二级缓存的策略是什么.
2). 集合级别的二级缓存的配置
I. 配置对集合使用二级缓存
<collection-cache usage="read-write" collection="com.atguigu.hibernate.entities.Department.emps"/>
也可以在 .hbm.xml 文件中进行配置
<set name="emps" table="GG_EMPLOYEE" inverse="true" lazy="true">
<cache usage="read-write"/>
<key>
<column name="DEPT_ID" />
</key>
<one-to-many class="com.atguigu.hibernate.entities.Employee" />
</set>
II. 注意: 还需要配置集合中的元素对应的持久化类也使用二级缓存! 否则将会多出 n 条 SQL 语句.
3). ehcache 的 配置文件: ehcache.xml
4). 查询缓存: 默认情况下, 设置的缓存对 HQL 及 QBC 查询时无效的, 但可以通过以下方式使其是有效的
I. 在 hibernate 配置文件中声明开启查询缓存
<property name="cache.use_query_cache">true</property>
II. 调用 Query 或 Criteria 的 setCacheable(true) 方法
III. 查询缓存依赖于二级缓存