现在的位置: 首页 > 综合 > 正文

SSH框架 – Hibernate基础讲解

2013年10月22日 ⁄ 综合 ⁄ 共 4338字 ⁄ 字号 评论关闭

Hibernate基础:

ORM与Hibernate

ORM(Object-Relation Map,对象关系映射):是一种解决面向对象编程过程中,程序与关系型数据库交互而提出的技术。简单来说ORM就是将程序中的对象与数据库中的表,对象的属性和表中的字段分别建立映射关系,通过映射关系,可以自由地通过操作对象来操作数据。

Hibernate就是一个开源的,轻量级的基于ORM的框架。

Hibernate的简单配置:

需要两个配置文件:
Hibernate主配置文件,在项目的src目录下

可以使用文件名为hibernate.cfg.xml也可以使用hibernate.properties

hibernate.cfg.xml的配置:

<?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>

              <property name="dialect">

                     org.hibernate.dialect.MySQLDialect

              </property>

              <property name="connection.url">

                     jdbc:mysql://localhost:3306/test

              </property>

              <property name="connection.username">root</property>

              <property name="connection.password">mysql</property>

              <property name="connection.driver_class">

                     com.mysql.jdbc.Driver

              </property>

              <property name="show_sql">true</property>

              <mapping resource="app/po/Student.hbm.xml" />

       </session-factory>

</hibernate-configuration>

hibernate.properties的配置:

       hibernate.dialect= org.hibernate.dialect.MySQLDialect

       hibernate. connection.driver_class= com.mysql.jdbc.Driver

       hibernate. connection.url= jdbc:mysql://localhost:3306/test

       hibernate. connection.username= root

       hibernate. connection.password= mysql

       hibernate. show_sql=true

在初始化Hibernate时,回首先读取hibernate.properties文件,如果没有就会加载hibernate.cfg.xml文件。

Hibernate映射文件:对象名.hbm.xml(对象名是与数据库表对应的对象类名)

Hibernata映射文件中指定了对象和数据库表,对象属性和表的字段的映射关系。映射的配置有两种方式:一种是xml,另一种就是Annotation

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">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="app.po.Student" table="student" catalog="test">

        <id name="student_id" type="java.lang.Integer">

            <column name="student_id" />

            <generator class="native"></generator>

        </id>

        <property name="student_name" type="java.lang.String">

            <column name="student_name" length="45" />

        </property>

        <!-- 定义PO类Student中的List类型的属性courses -->

              <list name="courses" table="course">

                     <key column="student_id" not-null="true" />

                     <list-index column="course_id" />

                     <element type="java.lang.String" column="course_name"/>

              </list>

              <!-- 单向一对多 -->

              <many-to-one name="teacher" column="teacher_id"

                     class="app.po.Teacher" not-null="true">

                     </many-to-one>

            

    </class>

</hibernate-mapping>

Hibernate高级

掌握映射文件的Annotation和xml两种配置

一、注意annotation的字段映射时:

1、类的属性不需要持久化是使用@Transient注解声明该字段是透明属性。

2、注意日期类型的精度是由@Temproal()指定,该注解有3个可选的value值用来指定日期精度。

3、对于枚举类型的映射使用@Enumerated()指定,该注解有两个可选参数用来指定存储时是按照值存储还是按照索引存储。

4、Hibernate字段映射的位置可以放在属性上或者方法上,但一般建议放在属性的get方法上。

5、使用@Table(name=””)来指定表名

6、使用@Column(name=””)来指定列名

二、映射的主键生成策略:

使用xml配置

用generator指定主键生成策略,常用的值有四个:native identity sequence和uuid。

使用注解的方式:

使用@Generatedvalue,默认值为auto相当于xml配置时的native。常用的值还有strategy=Table、identity和sequence。

了解主键sequence生成策略和TableGenerate生成策略

掌握联合主键的配置:

将联合主键组合为一个新的组件类对象,并实现Serializable接口,对该对象序列化。

重写equals()和hashCode()方法。

Xml配置

<composite-id name="" class="">

        <key-property name=""></key-property>

</composite-id>

     

       Annotation的配置

       1、将组件类注解为@Embeddable,然后将映射类中组件对象类的属性注解为@Id。

       2、直接在映射类中组件对象类的属性注解为@EmbeddedId。

       3、在为映射类添加注解@IdClass(),其值为需要作为主键的组件类名,然后为属性添加@Id注解。

三、hibernate的核心开发接口和三种状态:

a)        Configuration

包括annotationConfiguration

进行配置信息管理

生成SessionFactory

b)        SessionFactory

维护数据库的连接池

生成Session的两种方法

getCurrentSession()和openSession()

c)        Session

d)        

四、Hibernate映射

Xml配置:

a)        一对一:

1、  主键关联<one-to-one name=”” class=””>

2、  外键关联<many-to-one colume=”foreignkey” unique=”” >

b)        一对多:

1、  单向关联:<many-to-one>

2、  双向关联:在多的一方定义<many-to-one>;在另一方定义为

<set>

<key></key>

<one-to-many />

</set>

c)        多对多:

<set>

<key></key>

<many-to-many />

</set>

       Annotation配置:

a)        一对一

b)        一对多

c)        多对多

五、级联策略

Cascade

六、Hibernate查询:HQL和QBC

a)        HQL

b)        QBC

c)        数据检索策略

七、Hibernate过滤:

Session过滤

Filter过滤

抱歉!评论已关闭.