日期:2017/11/21
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hbernate的官网 http://hibernate.org/ 。提供了 5 个核心接口:Session 、SessionFactory 、Transaction 、Query 和 Configuration。

代码:
1、创建一个 Configuration 对象,并调用 configure() 方法,目的:读取配置文件(hibernate.cfg.xml),Hibernate的配置文件有两个:***.hbn.xml / hibernate.cfg.xml。
Configuration config = new Configuration().configure();
1.1 Configuration 对象
Class Configuration
- java.lang.Object
-
- org.hibernate.cfg.Configuration
-
public class Configuration extends Object
The approach here is to define all configuration and mapping sources in one API and to then build the SessionFactory in one-shot. The configuration and mapping sources defined here are just held here until the SessionFactory is built. This is an important distinction from the legacy behavior of this class, where we would try to incrementally build the mappings from sources as they were added. The ramification of this change in behavior is that users can add configuration and mapping sources here, but they can no longer query the in-flight state of mappings (PersistentClass, Collection, etc) here.
Note: Internally this class uses the new bootstrapping approach when asked to build the SessionFactory.
大意:Configuration 代表一种引导 Hibernate 的配置方法,这个方法在建立 SessionFactory 前,负责进行映射源的定义,配置Hibernate服务的信息。
1.2 configure() 方法 --- 方法重载
Configuration | configure()
Use the mappings and properties specified in an application resource named
hibernate.cfg.xml.
|
Configuration | configure(Document document)
Deprecated.
No longer supported.
|
Configuration | configure(File configFile)
Use the mappings and properties specified in the given application file.
|
Configuration | configure(String resource)
Use the mappings and properties specified in the given application resource.
|
Configuration | configure(URL url)
Use the mappings and properties specified in the given document.
|
大意:通过该方法,可以通过映射和配置来连接使用 hibernate.cfg.xml资源文件---用来配置java对象与关系数据库记录的映射关系。
2. 建立 SessionFactory 对象
SessionFactory sessionFactory = config.buildSessionFactory();
2.1 SessionFactory
The main contract here is the creation of Session instances. Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory.
The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This internal state includes all of the metadata about Object/Relational Mapping.
Implementors must be threadsafe.
大意:负责初始化Hibernate,可以被看作数据源代理,用于创建Session对象。SessionFactory是线程安全的,可以被多线程访问,一般创建一次(与Servlet被建立相似),单例模式。
Interface SessionFactory
-
-
All Superinterfaces:
- AutoCloseable, Closeable, EntityManagerFactory, HibernateEntityManagerFactory, Referenceable, Serializable
-
All Known Subinterfaces:
- SessionFactoryImplementor
-
All Known Implementing Classes:
- SessionFactoryDelegatingImpl, SessionFactoryImpl
public interface SessionFactory extends EntityManagerFactory, HibernateEntityManagerFactory, Referenceable, Serializable, Closeable
3、创建 Session 对象
Session session = sessionFactory.openSession();
3.1 session对象
public interface Session extends SharedSessionContract, EntityManager, HibernateEntityManager, AutoCloseable.The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)
The main function of the Session is to offer create, read and delete operations for instances of mapped entity classes.
大意:Session 接口继承了 SharedSessionContract, EntityManager, HibernateEntityManager, AutoCloseable 接口。主要用于搭建 java 应用与 Hibernate 交互的桥梁。是一个实现 持久服务 抽象化 的 主要API。Session生命周期 的界限是 逻辑事务的开始和结束。主要提供了以下功能:对映射的实体类进行 创建、读入、删除数据库操作。
Interface Session
-
-
All Superinterfaces:
- AutoCloseable, EntityManager, HibernateEntityManager, QueryProducer, Serializable, SharedSessionContract
-
All Known Subinterfaces:
- EventSource, SessionImplementor
-
All Known Implementing Classes:
- AbstractDelegateSessionImplementor, AbstractSessionImpl, SessionDelegatorBaseImpl, SessionImpl, ToOneDelegateSessionImplementor
public interface Session extends SharedSessionContract, EntityManager, HibernateEntityManager, AutoCloseable
4、创建 Transaction 事务
Transaction tx = session.beginTransaction();
4.1 Transaction 对象
Defines the contract for abstracting applications from the configured underlying means of transaction management. Allows the application to define units of work, while maintaining abstraction from the underlying transaction implementation (eg. JTA, JDBC).
A transaction is associated with a Session and is usually initiated by a call to SharedSessionContract.beginTransaction(). A single session might span multiple transactions since the notion of a session (a conversation between the application and the datastore) is of coarser granularity than the notion of a transaction. However, it is intended that there be at most one uncommitted transaction associated with a particular Session at any time.
Implementers are not intended to be thread-safe.
大意: Transaction 定义了一个协议给 事务管理工具,从而 在保持数据库驱动被抽象化使用下, 允许应用定义单元工作(就是有条不紊的工作)。一个 transaction 是连接在 一个 Session 上的,常常通过 调用 beginTransaction() 方法 而初始化。一个 Session 可以对应多个transaction ,事务是非线程安全的。
Interface Transaction
-
-
All Superinterfaces:
- EntityTransaction
-
All Known Subinterfaces:
- TransactionImplementor
-
All Known Implementing Classes:
- TransactionImpl
public interface Transaction extends EntityTransaction
5、完成所有持久化操作与事务操作后,需要关闭Session 与 sessionFactory。

本文介绍了Hibernate框架的基本使用流程,包括创建Configuration对象、建立SessionFactory、创建Session和Transaction等关键步骤。
1457

被折叠的 条评论
为什么被折叠?



