java(33):Hibernate框架(1):基础

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

日期: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 对象

org.hibernate.cfg

Class Configuration



  • public class Configuration
    extends Object
     Represents one approach for bootstrapping Hibernate. In fact, historically this was the way to bootstrap Hibernate.
     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.
Use the mappings and properties specified in an application resource named  hibernate.cfg.xml .

 大意:通过该方法,可以通过映射和配置来连接使用 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被建立相似),单例模式。


org.hibernate

Interface SessionFactory

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生命周期  界限是 逻辑事务的开始和结束。主要提供了以下功能:对映射的实体类进行 创建、读入、删除数据库操作。


org.hibernate

Interface Session

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 ,事务是非线程安全的。

org.hibernate

Interface Transaction


5、完成所有持久化操作与事务操作后,需要关闭Session 与 sessionFactory。 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值