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

来源:互联网 发布:java手机游戏安卓版 编辑:程序博客网 时间:2024/06/05 14:36

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

  • java.lang.Object
    • org.hibernate.cfg.Configuration


  • public class Configurationextends 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() 方法 --- 方法重载

Configurationconfigure()

Use the mappings and properties specified in an application resource named hibernate.cfg.xml.
Configurationconfigure(Document document)
Deprecated. 
No longer supported.
Configurationconfigure(File configFile)
Use the mappings and properties specified in the given application file.
Configurationconfigure(String resource)
Use the mappings and properties specified in the given application resource.
Configurationconfigure(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

  • All Superinterfaces:
    AutoCloseable, Closeable, EntityManagerFactory, HibernateEntityManagerFactory, Referenceable, Serializable
    All Known Subinterfaces:
    SessionFactoryImplementor
    All Known Implementing Classes:
    SessionFactoryDelegatingImpl, SessionFactoryImpl


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


org.hibernate

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

org.hibernate

Interface Transaction

  • All Superinterfaces:
    EntityTransaction
    All Known Subinterfaces:
    TransactionImplementor
    All Known Implementing Classes:
    TransactionImpl


    public interface Transactionextends EntityTransaction

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


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 生完孩子没奶水怎么办 梦见让狐狸咬了怎么办 1岁宝宝不吃辅食怎么办 母乳不够宝宝不吃奶粉怎么办 吃母乳的宝宝不吃奶粉怎么办 宝宝吃母乳不吃奶粉怎么办 1岁婴儿不吃辅食怎么办 不喝奶瓶的宝宝怎么办 母乳不足宝宝不吃奶粉怎么办 4岁宝宝注意力不集中怎么办 孩子上课不专心听讲怎么办 小孩上课不认真听讲怎么办 一年级孩子上课不认真听讲怎么办 打了孩子很自责怎么办 4岁宝宝讲话结巴怎么办 网销客户不说话怎么办 两岁宝宝不说话怎么办? 4岁儿童说话结巴怎么办 3岁宝宝说话结巴怎么办 6岁儿童舌头短怎么办 5岁宝宝说话结巴怎么办 两岁宝宝说话晚怎么办 6岁儿童说话结巴怎么办 2岁宝宝呕吐拉稀怎么办 2岁宝宝突然呕吐怎么办 2岁宝宝呕吐发烧怎么办 2岁宝宝呕吐厉害怎么办 1岁宝宝半夜呕吐怎么办 2岁半宝宝呕吐怎么办 2岁宝宝半夜呕吐怎么办 两岁宝宝一直吐怎么办 原画师老了以后怎么办 孩子不想上学怎么办怎么去说服 嫉妒别人比我好怎么办 三岁宝宝爱打人怎么办 1岁宝宝喜欢打人怎么办 ps图层解锁不了怎么办 沈腾结婚马丽怎么办 延长甲没有纸托怎么办 高考第一志愿没录取怎么办 电子画颜料干了怎么办