hibernate运行机制
来源:互联网 发布:tensorflow 入门实例 编辑:程序博客网 时间:2024/04/26 09:35
使用hibernate也有段时间了,以前都没有写相关的学习记录,最近面试被问到相关问题,现在有点空按个人的理解回头总结一下hibernate的运行机制,当做是一种复习吧。
Hibernate运行机制:
1、读取并解析配置文档
- Configuration cfg = new Configuration().configure();
Configuration cfg = new Configuration().configure();
2、读取并解析映射文件,创建SessionFactory
- SessionFactory sf = cfg.buidSessionFactory();
SessionFactory sf = cfg.buidSessionFactory();
3、打开Session
- Session session = sessionFactory.openSession();
Session session = sessionFactory.openSession();
4、创建事务Trasction
- Transaction tx = session.beginTransaction();
Transaction tx = session.beginTransaction();
5、持久化操作
- User user = new User();
- user.setBirthday(new Date());
- user.setName("name");
- session.save(user);
User user = new User();user.setBirthday(new Date());user.setName("name");session.save(user);
6、提交事务
- tx.commit();
tx.commit();
7、关闭Session
- session.close();
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;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();//读取并解析映射文件,创建SessionFactorySessionFactory sf = cfg.buidSessionFactory();//打开sessionSession session = sf.openSession();//开启事务Transaction tx = session.beginTransaction();//持久化操作User user = new User();user.setBirthday(new Date());user.setName("name");session.save(user);//提交事务tx.commit();//关闭Sessionsession.close();//关闭SessionFactorysf.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;
- }
- }
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">
- <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>
<?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>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>
<!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
- hibernate运行机制
- hibernate运行机制
- hibernate运行机制
- hibernate运行机制
- Hibernate运行机制
- Hibernate运行机制:。
- Hibernate运行机制分析
- 运行机制
- web项目加入Struts,Hibernate,Spring后的运行机制
- Hibernate框架运行机制(增删改查,自动生成表,query接口,criteria接口,关系映射,缓存)
- 计算机病毒运行机制
- java运行机制
- Cookie运行机制
- Subscriptions 运行机制
- CLR运行机制
- .NET运行机制
- Subscriptions 运行机制
- session运行机制
- Windows下gvim配置
- Getting Started with the Aircrack-Ng Suite of Wi-Fi Hacking Tools
- Hello world”学起来的,
- linux awk命令详解
- Android开发之EditText属性详解
- hibernate运行机制
- 单例模式
- 讲讲10个对开发项目有害的编程习惯
- SSH连接Linux
- C++ 结构体定义及使用
- Xposed笔记(一)Xposed初识
- 访问google
- xml模式文档(xml:Schema)详解
- 统计字符串的单词数