1.Hibernate初识

来源:互联网 发布:漫研社淘宝 编辑:程序博客网 时间:2024/04/29 06:50

Hibernate初识

什么是ORM

 

Hibernate简介

 

 

 

创建Hibernate项目

l 导入Hibernate必须的jar

l 位于\hibernate-release-x.x.x.x\lib\required 文件夹下

 

l 导入Mysqld的JDBC驱动

 

编写第一个Hibernate例子思路

1.创建Hibernate的配置文件

2.创建持久化类

3.创建ORM(对象关系映射)文件

4.通过Hibernate API 编写访问数据库的代码

 

 

 

 

 

 

Session详解

openSession getCurrentSession的区别

l openSession每次创建新的session对象,getCurrentSessin使用现用的session对象(第一次使用此时自动产生的session对象,注:使用了单例模式)

 

l openSession在事务提交或者回滚之后需要手动关闭,如果没有手动关闭将导致连接池溢出。而getCurrentSession会自动关闭。

 

l 在使用上,getCurrentSession要在hibernate.cfg.xml中配置事务,而openSession不需要

 

 

附:

getCurrentSession要在hibernate.cfg.xml中配置事务:

方式一:使用本地事务(jdbc事务)

<property name=”hibernate.current_session_context_class”>thread</property>

方式二:使用全局事务(jta事务)

<property> name=”hibernate.current_session_context_class”>jta</property>

 

0 0
原创粉丝点击