---
title: "hibernate学习笔记(五)延遲初始 hibernate.cfg.xml文件的具体配置"
author: "wangle860102"
date: "2008-05-26T11:30:49Z"
tags: []
slug: "hibernate-hibernate-cfg-xml"
draft: false
lastmod: "2008-05-26T11:30:49Z"
id: 196948
url: "https://wangle.iteye.com/blog/196948"
---

hibernate学习笔记(五)延遲初始 Lazy Initialization

2008年01月30日 星期三 00:09

在Hibernate中，集合類的映射可以延遲初始（Lazy Initialization），在多对一或者一对多中，都可以使用延遲初始，例如：一个用户(user对应user表)有多个email地址(address对应address表)，也就是在真正索取該物件的資料時，才向資料庫查詢，就上次例子來說，就是我們在讀取User時，先不取得其中的 addrs屬性中之物件資料，由於只需要讀取User的name屬性，此時我們只要執行一次select即可，真正需要addrs的資料時，才向資料庫要求。在含有集合类的user.hbm.xml中要如下设置：

　　&lt;set name="addrs" table="ADDRS" lazy="true"&gt;  
              &lt;key column="USER\_ID"/&gt;  
              &lt;element type="string" column="ADDRESS" not-null="true"/&gt;  
          &lt;/set&gt;

[hibernate学习笔记(一)hibernate.cfg.xml文件的具体配置](http://blog.csdn.net/wangle860102/archive/2008/05/23/2472418.aspx)

 

hibernate学习笔记(一)hibernate.cfg.xml文件的具体配置

一、首先学习hibernate.cfg.xml配置文件的具体配置  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;!--指定该文件的官方dtd--&gt;  
&lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "[http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd](http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd)" &gt;  
&lt;hibernate-configuration&gt;  
   &lt;session-factory&gt;  
    &lt;!-- 显示sql语言 --&gt;  
     &lt;property name="show\_sql"&gt;true&lt;/property&gt;  
    &lt;!-- sql语言 --&gt;  
     &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;  
    &lt;!-- jdbc驱动程式 --&gt;  
     &lt;property name="connection.driver\_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;  
    &lt;!-- jdbc url --&gt;  
     &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/test&lt;/property&gt;  
    &lt;!-- 数据库用户名 --&gt;  
     &lt;property name="connection.username"&gt;root&lt;/property&gt;  
    &lt;!-- 数据库密码 --&gt;  
     &lt;property name="connection.password"&gt;wyq&lt;/property&gt;  
    &lt;!-- C3P0连接池设定 --&gt;  
     &lt;!--最小连接数--&gt;  
     &lt;property name="c3p0.min\_size"&gt;5&lt;/property&gt;  
    &lt;!--最大连接数--&gt;  
     &lt;property name="c3p0.max\_size"&gt;20&lt;/property&gt;  
    &lt;!--延迟所允许的时间--&gt;  
     &lt;property name="c3p0.timeout"&gt;1800&lt;/property&gt;  
   &lt;!--缓存所允许的最大连接数--&gt;  
     &lt;property name="c3p0.max\_statements"&gt;50&lt;/property&gt;  
    &lt;!-- 每隔100笔资料送入资料库，清除缓存（定期清除缓存，减小压力） --&gt;  
     &lt;property name="hibernate.jdbc.batch\_size"&gt;100&lt;/property&gt;  
    &lt;!-- 设定事务管理的工厂类 --&gt;  
     &lt;property name="hibernate.transaction.factiory\_class"&gt;org.hibernate.transaction.JDBCTransactionFactory&lt;/property&gt;  
    &lt;mapping resource="com/wyq/hibernate/pojo/User.hbm.xml"/&gt;  
    &lt;mapping resource="com/wyq/hibernate/pojo/TUser.hbm.xml"/&gt;  
    &lt;mapping resource="com/wyq/hibernate/pojo/Room.hbm.xml"/&gt;  
   &lt;/session-factory&gt;  
&lt;/hibernate-configuration&gt;  
需要的jar包有c3p0.jar,hibernate3.jar,数据库.jar,log4j.jar