Hibernate运行机制

1、读取并解析配置文档

Configuration cfg = new Configuration().configure();

2、读取并解析映射文件,创建SessionFactory

SessionFactory sf = cfg.buidSessionFactory();

3、打开Session

Session session = sessionFactory.openSession();

4、创建事务Trasction

Transaction tx = session.beginTransaction();

5、持久化操作

User user = new User();

user.setBirthday(new Date());

user.setName("name");

session.save(user);

6、提交事务

 

tx.commit();

7、关闭Session

 

session.close();

8、关闭SessionFactory

 

以下是个简单的例子:

 

package cn.flymouse.hibernate;

 

import java.util.Date;

 

 

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import cn.flymouse.hibernate.domain.User;

 

public class Base {

 

    public static void main(String[] args) {

        //读取并解析配置文档

        Configuration cfg = new Configuration().configure();

        //读取并解析映射文件,创建SessionFactory

        SessionFactory sf = cfg.buidSessionFactory();

        //打开session

        Session session = sf.openSession();

        //开启事务

        Transaction tx = session.beginTransaction();

        //持久化操作

        User user = new User();

        user.setBirthday(new Date());

        user.setName("name");

        session.save(user);

        //提交事务

        tx.commit();

        //关闭Session

        session.close();

        //关闭SessionFactory

        sf.close();

    }

}

 

User类: 

 

package cn.flymouse.hibernate.domain;

 

import java.util.Date;

 

public class User {

    private int id;

    private String name;

    private Date birthday;

 

    public int getId() {

        return id;

    }

    pubilc String getName(){

        return name;

    }

    public Date getBirthday() {

        return birthday;

    }

    

    public void setId(int id) {

        this.id = id;

    }

    public void setName(String name) {

        this.name = name;

    }

    public void setBirthday(Date birthday) {

        this.birthday = birthday;

    }

}

 

User的映射文件User.hbm.xml(跟User类放同个目录): 

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC 

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

//dtd就是相当于倒包。引进资源包。

<hibernate-mapping package="cn.flymouse.hibernate.domain">

 

    <class name="User">

        <id name="id">

            <generator class="native" />

        </id>

        <property name="name" unique="false"/>

        <property name="birthday" />

    </class>

    

</hibernate-mapping>

hibernate的配置文件hibernate.cfg.xml(放在类路径下,即src目录下): 

 

<!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="connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql:///test</property>

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

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

        <!-- 方言,用于生成SQL语句 -->

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 自动建表 -->

        <property name="hbm2ddl.auto">create</property>

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

    

        <mapping resource="cn/flymouse/hibernate/domain/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 

Hibernate中的三种对象状态:

 

1.瞬时(transient):数据库中没有数据与之对应,超过作用域会被JVM垃圾回收器回收,一般是new出来且与session没有关联的对象。

2.持久(persistent):数据库中有数据与之对应,当前与session有关联,并且相关联的session没有关闭,事务没有提交;持久对象状态发生改变,在事务提交时会影响到数据库(hibernate能检测到)。

3.脱管(detached):数据库中有数据与之对应,但当前没有session与之关联;托管对象状态发生改变,hibernate不能检测到。


Hibernate对比Jdbc:
1、对JDBC访问数据库的代码做了封装,大大简化了数据访问繁琐的代码;
2、Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现,他很大程度的简化DAO曾的编码工作;
3、Hibernate使用Java发射机制,而不是字节码,增强程序来实现透明性;
4、Hibernate的性能非常好,因为他是一个轻量级框架,框架的灵活性很突出。它支持各种关系数据库从一对一到多对多的各种复杂工作;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值