看hibernate原理图如下:hibernate的设计包含三步:实体类设计,实体类映射文件编写,hibernate配置文件编写。
一、实体类设计
以前的面向过程编程,和数据库打交道,我们使用datatable ,dataset等之类的,逐步的开始接触面向对象设计,开始运用面向对象的思想来包装自己的代码。面向对象的设计理念,一切皆面向对象。所以实体类设计是面向对象的核心。
通过实体在业务逻辑中的传递,才得以将“需求数据”保存到数据库中,项目中的实体是将用户需求和数据(database)连接起来的桥梁。
关于面向对象实体设计,这里不是重点要说的,hibernate的实体设计就是我们平常的实体设计。
看unit的一个实例:
using System;
using System.Collections;
namespace UIEntity
{
public class UnitEntity
{
protected int _unitId;
/// <summary>
/// </summary>
public virtual int unitId
{
get
{
return _unitId;
}
set
{
_unitId = value;
}
}
protected String _unitName;
/// <summary>
/// </summary>
public virtual String unitName
{
get
{
return _unitName;
}
set
{
if (value != null && value.Length > 254)
throw new ArgumentOutOfRangeException("Invalid value for unitName", value, value.ToString());
_unitName = value;
}
}
protected String _unitDesc;
/// <summary>
/// </summary>
public virtual String unitDesc
{
get
{
return _unitDesc;
}
set
{
if (value != null && value.Length > 254)
throw new ArgumentOutOfRangeException("Invalid value for unitDesc", value, value.ToString());
_unitDesc = value;
}
}
protected String _address;
/// <summary>
/// </summary>
public virtual String address
{
get
{
return _address;
}
set
{
if (value != null && value.Length > 254)
throw new ArgumentOutOfRangeException("Invalid value for address", value, value.ToString());
_address = value;
}
}
protected IList _personEntities;
public virtual IList personEntities
{
get
{
if(_personEntities==null)
{
_personEntities=new ArrayList();
}
return _personEntities;
}
set
{
_personEntities = value;
}
}
}
}
二、实体类映射文件的编写
实体类映射文件以“hbm.xml"结尾。区别于配置文件“cfg.xml”。
为什么会出现映射文件,上篇博客也提到:hibernate之所以可以将实体类持久化到数据库,这些实体映射文件是大功臣,hibernate通过这些“约定”文件来读写实体信息,然后写入的数据库。
原则:实体类映射文件和要映射到数据库的实体类是对应的。
也就是说:如果需要把某个实体类持久化到数据库中,那么这个类必定存在一个映射文件;但是实体类和映射文件不是一一对应的,多个实体类可以通过一个映射文件来持久化到数据库。
表现形式:
一个实体类一个映射文件
多个实体类,一个映射文件
1、一个实体用一个映射文件来表示:针对实体类Unit,我们需要一个Unit.hbm.xml。
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="UIEntity.UnitEntity, UIEntity" table="UnitEntity">
<id name="unitId" type="Int32" unsaved-value="null">
<column name="unitId" length="4" not-null="true" unique="true"/>
<generator class="native" />
</id>
<property name="unitName" type="String">
<column name="unitName" length="254" not-null="false"/>
</property>
<property name="unitDesc" type="String">
<column name="unitDesc" length="254" not-null="false"/>
</property>
<property name="address" type="String">
<column name="address" length="254" not-null="false"/>
</property>
<bag name="personEntities" inverse="true" lazy="false" cascade="all-delete-orphan">
<key column="unitId"/>
<one-to-many class="UIEntity.PersonEntity, UIEntity"/>
</bag>
</class>
</hibernate-mapping>
大多数情况,我们都使用这种一对一的形式来表示,但是继承映射中,我们可以有另一种形式(继承映射有三种形式,这里不做重点介绍,只是介绍同一个hbm.xml文件映射多个实体类的例子)。
2、多个实体类,一个映射文件
背景:有一个动物的抽象类Animal,Animal下有两个子类:pig,bird。我们目的是把具体类映射成数据表。因为Animal是一个抽象的类,所以我们不映射它,映射两个子类到两个数据表中。
在这个xml中我们通过“abstract”来标志Animal这个类不需要映射成数据库表,而bird,pig则需要映射成数据库表的写法
<?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="cassie.Hibernate">
<class name="Animal" abstract="true" >
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="sex"/>
<union-subclass name="Pig" table="t_pig">
<property name="weight"/>
</union-subclass>
<union-subclass name="Bird" table="t_bird">
<property name="height"/>
</union-subclass>
</class>
</hibernate-mapping>
三、hibernate配置文件的编写,我们在上篇博客中提到过就是cfg.xml文件的编写。
Hibernate.cfg.xml可包含的内容有很多,主要分为三部分:
1、数据库相关的————必要信息
数据库驱动:hibernate.connection.driver_class
数据库:hibernate.connection.url
用户名:hibernate.connection.username
密码:hibernate.connection.password
数据库方言:hibernate.dialect
2、常用配置信息
<!-- 配置缓存提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 启用二级缓存,这也是它的默认配置 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
…………
3、映射文件信息——必要
<mapping resource="cassie/Hibernate/CollectionList.hbm.xml"/>
有了映射文件地址,这个hibernate的配置才算完整,不然,我们的实体类一个都映射不到。
常用实例如下:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Hibernate_collection</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
<property name="hibernate.show_sql">true</property>
<mapping resource="cassie/Hibernate/CollectionList.hbm.xml"/>
</session-factory>
</hibernate-configuration></span>
以上就是使用hibernate的主要三步任务。其实写完博客,觉得只有两步,就是两中xml文件的编写。因为实体类无论如何都是要设计的。而这两种xml文件的作用,就是hibernate中最为明显的规则:约定。Xml的编写是遵循了hibernate已制定好的规范进行编写,才能被识别,被映射到数据库。到此是对hibernate xml文件的认识,从宏观上掌握明白hibernate的xml跟以前的用过的xml的区别:就是多几个有关hibernate的标签而已。
下篇博客介绍这些xml文件中包含元素的含义。