SessionFactory:

  a)  用来产生和管理Session

  b)通常情况下每个应用只需要一个SessionFactory

  c)除非要访问多个数据库的情况

  d) 关注两个方法即:  openSession    和 getCurrentSession

     i.   openSession 每次都是新的,需要close

     ii.  getCurrentSession 从上下文找,如果有,用旧的,如果没有,建新的

         1,用途,界定事务边界

         2,事务提交自动close

         3,跟current_session_context_class (JTA、thread) 有关系

            a)thread 使用 connection

注:上下文是什么?当前session运行的上下文,在hibernate.cfg.xml里指定的:

    最常用的就是thread,看当前的线程里有没有session对象,有了就拿,没有就建新的。

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

SessionFactory怎么理解?

SessionFactory,产生session的工厂。一般情况下,每一个session里都有一个数据库的连接,(如果没有数据库连接,怎么和数据库打交道呢?)所以产生session的工厂里边一定有好多个数据库连接,所以SessionFactory维护的最重要的东西就是数据库连接池。当它产生一个session的时候,他会从数据库连接池拿出一个连接交给session。

package com.oracle.hibernate.id;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test; public class HibernateCoreAPITest { private static SessionFactory sf = null; @BeforeClass
public static void beforeClass() {
//try-chatch是为了解决Junit有错误不提示的bug
try {
/**
* AnnotationConfiguration().configure()会默认找src下的hibernate.cfg.xml,读取配置信息。
* 最主要的是数据库连接信息,帮你做出连接池。SessionFactory代表着跟数据库的连接
* configure()有个重载方法configure(String url)可以指定文件名;如果配置文件叫hibernate.xml,
* 就configure("hibernate.xml"),默认去classpath下找配置文件
*/
sf = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (HibernateException e) { e.printStackTrace();
}
} @Test
public void testTeacher() { Teacher t = new Teacher(); t.setName("li"); t.setTitle("high");
/**
* getCurrentSession():
*1, 如果当前上下文有session,就拿来当前的session。如果没有,就创建一个新的session
* 2,不写session.close();
* 注意:当session commit()提交的时候自动close(),写了会报错。
* 提交同时也会关闭当前session,上下文将不存在该session,此时getCurrentSession()就会创建一个新的session。
*/
Session session = sf.getCurrentSession(); /**
* session有个connection方法,返回值也是Connection,可以看出在这个session里绑定了一个Connection,
* 这个Connection是从SessionFactory通过配置文件信息,产生的数据库连接池里取出的。所以简单理解,session等
* 同于一个Connection,不过也不能这个理解,hibernate还有很多很复杂的东西。
*/
//session.connection(); //openSession(),永远都打开新的session。需要close();
//Session session = sf.openSession(); session.beginTransaction();
session.save(t); //当当前的session提交了,就关闭了,对象就没了。再getCurrentSession就是新的。
//session.getTransaction().commit();
//session.close(); //Session session2 = sf.openSession();
//两次opSession不是一个session,返回false
//System.out.println(session == session2); //Session session2 = sf.getCurrentSession();
//如果session没commit,返回true,否则返回false
//System.out.println(session == session2); }
@AfterClass
public static void afterClass() {
sf.close();
} }

Hibernate核心开发接口_SessionFactory详解的更多相关文章

  1. Hibernate学习笔记2.5(Hibernate核心开发接口和三种状态)

    1.configuration(配置信息管理,产生sessionfactory) sessionfactory管理一系列的连接池 opensession 永远打开新的,需要手动close getcur ...

  2. SQLite3开发接口函数详解

    SQLite3是SQLite一个全新的版本,它虽然是在SQLite 2.8.13的代码基础之上开发的,但是使用了和之前的版本不兼容的数据库格式和API. SQLite3是为了满足以下的需求而开发的: ...

  3. hibernate核心开发接口_Configuration

    AnnotationConfiguration继承自Configuration,这里以AnnotationConfiguration为例: new AnnotationConfiguration(). ...

  4. 5.Hibernate 核心开发接口

    一.Configuration(AnnotationConfiguration) 作用:进行配置信息的管理 目标:用来产生SessionFactory 可以在configure 方法中指定hibern ...

  5. C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解

    系列目录     [已更新最新开发文章,点击查看详细] 在我的博客<C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解>最后列出了 Fil ...

  6. HIbernate学习笔记(二) hibernate对象的三种状态与核心开发接口

    1.在hibernate中持久化对象有三个状态,这个面试时可能会问到: (1)transient瞬时态:在数据库中没有与之匹配的数据,一般就是只new出了这个对象,并且在session缓存中也没有即此 ...

  7. WebService核心之WSDL深入详解

    WebService核心之WSDL深入详解 根据上一篇文章开发的Web Service实例生成的WSDL文档如下: XML里两个属性介绍: targetNamespace          相当于ja ...

  8. Hibernate配置文件和映射文件详解

    Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架. 我们先看一下官方文档所给出的,Hibernate 体系结构的高层视图: 其中PO=P ...

  9. 【转】maven核心,pom.xml详解

    感谢如下博主: http://www.cnblogs.com/qq78292959/p/3711501.html maven核心,pom.xml详解 什么是pom?    pom作为项目对象模型.通过 ...

随机推荐

  1. 看图说说JVM GC收集算法

  2. Deep learning for visual understanding: A review

    https://www.sciencedirect.com/science/article/pii/S0924271618301291?dgcid=raven_sd_recommender_email ...

  3. Android-bindService本地服务-初步-Service返回对象

    在Android开发过程中,Android API 已经有了startService方式,为什么还需要bindService呢? 答:是因为bindService可以实现Activity-->S ...

  4. java性能分析工具 jconsole.exe

    通过 Java visualMv结合 jconsole.exe   工具即可查看如图所示(Jconsole在JDK文件夹内,非JRE文件夹) 在Java Visualvm工具里面安装JTA插件,分析线 ...

  5. (zxing.net)一维码Code 93的简介、实现与解码

    一.简介 一维码Code 93: Code 93码与Code 39码的字符集相同,但93码的密度要比39码高,因而在面积不足的情况下,可以用93码代替39码.它没有自校验功能,为了确保数据安全性,采用 ...

  6. uploadPreview 上传图片前预览 IE9 索引无效的问题

    最近公司的项目用到比较多的上传图片的操作,所以用到了基于jquery的上传前预览的插件 uploadPreview ,后来测试的时候发现在IE9下报索引无效的问题. 异常的产生方式 放一个file控件 ...

  7. tcp udp socket编程

    http://blog.csdn.net/ns_code/article/details/14128987

  8. java学习笔记—HttpServletResponse(21)

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, ...

  9. 【ocp-12c】最新CUUG OCP-071考试题库(48题)

    48.(10-12)choose the best answer View the Exhibit and examine the description for the PRODUCTS and S ...

  10. 护网杯圆满结束,还不满足?不如来看看大佬的WP扩展思路~

    护网杯预选赛 WP转载自:https://qingchenldl.github.io/2018/10/13/%E6%8A%A4%E7%BD%91%E6%9D%AFWP-BitPwn/#more WEB ...