使用hibernate也有段时间了,以前都没有写相关的学习记录,最近面试被问到相关问题,现在有点空按个人的理解回头总结一下hibernate的运行机制,当做是一种复习吧。
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();
- }
- }
- 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;
- }
- }
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.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>
- <!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的性能非常好,因为他是一个轻量级框架,框架的灵活性很突出。它支持各种关系数据库从一对一到多对多的各种复杂工作;