第一讲 Hibernate 简介

 

Hibernate框架:ORM(对象关系映射)—— 完成数据库的相关操作。

概念:

持久化:将内存中的对象,自动映射到数据库。

完成持久化技术:

主动域模式:

代表作:EJB3.X(官方的持久化)

ORM(对象关系模型):

代表作:Hibernate、IBaits、JPA(sun官方)

JDO模型:

CMP模型:

代表作:EJB3.X(官方的持久化)

Hibernate API简介

Configuration —— 用来加载配置文件,返回SessionFactory。

SessionFactory —— 用来初始化Hibernate(读取hibernate.cfg.xml配置文件),返回session对象。(类似:DBUtil)

Session —— 用来持久化数据的。(完成数据的相关操作CRUD)

Transaction —— 用来管理事务。(Hibernate要求增、删、改必须使用事务操作)

事务自动提交:hibernate.cfg.xml文件中配置:connection.autocommit = true;

Query —— 用来完成查询操作。(注意:Query中只能使用Hibernate中特有的查询语句——HQL)

常用方法:

save(Object obj) —— 用来保存对象。(内存中的对象,持久化到数据库;添加)

update(Object obj) —— 用来修改对象。(必须先将数据库中的对象,映射到内存中,才能执行修改; 必须先查询对象,再修改)

delete(Object obj) —— 用来删除对象。(必须先将数据库中的对象,映射到内存中,才能执行删除; 必须先查询对象,再删除)

get(Class,主键)—— 用来查询数据。

marge(Object obj ) —— 强制关联持久层对象与数据库。

 

手动完成Hibernate的相关操作:

1. 创建Configuration实例。

2. 创建SessionFactory实例。

3. 创建Session实例。

4. 设置Session中事务。

5. 操作数据。

6. 提交或回滚事务。

7. 关闭Session。

demo:

 

package com.sec.util;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

    private static Configuration cfg=null;
    
    static{
        cfg=new Configuration().configure();
    }
    
    public static SessionFactory getSessionFactory(){
        return cfg.buildSessionFactory();
    }
    
    public static void save(Object obj){
        Session session=getSessionFactory().openSession();
        Transaction tran=session.getTransaction();
        tran.begin();
        session.save(obj);
        tran.commit();
        session.close();
    }
    public static Object qeury(Object obj,Serializable where){
        Session session=getSessionFactory().openSession();
        obj=session.get(obj.getClass(), where);
        session.close();
        return obj;
    }
}

 

public static void main(String[] args) {
  TypeInfo t=new TypeInfo();
  t=(TypeInfo)HibernateSessionFactory.qeury(t, 3);
  CsdnInfo model=new CsdnInfo(t, "我是Hibernate", "...", 2, 0,new Timestamp(System.currentTimeMillis()),"备注");
  HibernateSessionFactory.save(model);
}
<?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.SQLServerDialect
    </property>
    <property name="connection.url">
        jdbc:sqlserver://localhost:1433;databaseName=dataDB
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password">123456</property>
    <property name="connection.driver_class">
        com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>
    <property name="myeclipse.connection.profile">ljConn</property>
    <property name="connection.autocommit">true</property><!--设置事务自动提交-->
    <mapping resource="com/sec/model/TypeInfo.hbm.xml" />
    <mapping resource="com/sec/model/CsdnInfo.hbm.xml" />

</session-factory>

</hibernate-configuration>

posted on 2012-11-05 09:04  somewhere!  阅读(209)  评论(0编辑  收藏  举报

导航