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 对象
Class Configuration
- java.lang.Object
- org.hibernate.cfg.Configuration
public class Configurationextends 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()
Configuration
configure(Document document)
Configuration
configure(File configFile)
Configuration
configure(String resource)
Configuration
configure(URL url)
大意:通过该方法,可以通过映射和配置来连接使用 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 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生命周期 的界限是 逻辑事务的开始和结束。主要提供了以下功能:对映射的实体类进行 创建、读入、删除数据库操作。
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 ,事务是非线程安全的。
Interface Transaction
- All Superinterfaces:
- EntityTransaction
- All Known Subinterfaces:
- TransactionImplementor
- All Known Implementing Classes:
- TransactionImpl
public interface Transactionextends EntityTransaction
5、完成所有持久化操作与事务操作后,需要关闭Session 与 sessionFactory。
- java(33):Hibernate框架(1):基础
- Java框架之hibernate(1)
- Hibernate框架(1)
- Hibernate基础(1)
- Hibernate(基础1)
- Java框架之hibernate(2)
- Java框架之hibernate(3)
- hibernate框架(篇1)
- 深入浅出学习Hibernate框架(二):JDBC基础操作
- 深入浅出学习Hibernate框架(二):JDBC基础操作
- 深入浅出学习Hibernate框架(二):JDBC基础操作
- java基础(七)------java集合框架
- java-hibernate框架介绍1
- Struts + Spring + Hibernate基础框架搭建1
- Java框架--- hibernate 框架
- Hibernate框架基础入门
- Hibernate框架基础
- Hibernate框架基础
- Html之opener对象解决父子tab页信息交换-yellowcong
- Map分支-HashMap
- 18-EMM Procedure 6. Handover without TAU
- 理解Android中的MVP架构
- HashSet
- java(33):Hibernate框架(1):基础
- hive 优化(二)
- springboot 生成二维码
- wpf基于帧的动画
- MySQL数据类型之日期和时间类型
- 终止占有8080端口的Tomcat进程
- 19-EMM Procedure 7. Cell Reselection without TAU
- JS 委托事件
- 一个01字符串,求出现0、1出现次数相等的最长子串