1。当我们调用

Configuration config=new Configuration().configure();

时候Hibernate会自动在当前的CLASSPATH中搜寻hibernate.cfg.xml文件并将其读取到内存作为后继操作的基础配置。

我们也可以指定配置文件名,如果不希望使用默认的hibernate.cfg.xml文件作为配置文件的话:

SessionFactory

SessionFactory 负责创建Session的实例。我们可以通过Configuation实例创建SessionFactory;

Configuration config=new Configuration().configure();

SessionFactory sessionFactory=config.buildSessionFactory();

Configuration 实例config 会根据当前的配置信息,构造SessionFactory实例并返回。SessionFactory一旦构建完毕,即被赋予特定的配置信息。也就是说,之后config的任何变更将不会影响到已经创建的SessionFactory实例(SessionFactory)。如果需要使用基于改动后的config实例的SessionFactory,需要从config重新构建一个SessionFactory实例。

Session

Session 是持久层操作的基础,相当于JDBC中的Connnection。

Configuration config=new Configuration().configure();  //读取默认的hibernate.cfg.xml的文件

SessionFactory sessionFactory=config.buildSessionFactory();  //通过config创建sessionFactory的实例

Session session=sessionFactory.openSession();       //获得session

之后,我们就可以调用Session所提供的save,find,flush等方法完成持久化操作:

看例Save:

TUser user=new TUser();

user.setName("yuanliang");

session.save(user);

session.flush();//Session.flush方法强制数据库同步,这里既强制Hibernate将user实例立即同步到数据库中。在事物提交的时候和Session关闭的时候,也会自动执行flush方法

find()//这个方法返回一个List

List list=Session.find(From TUser as tu order by tu.name asc);

Load() //这个方法返回是一个对象

Customer c=(Customer)session.load(Customer.class,customer_id);

修改数据:

1. 将要修改的数据从数据库读出,赋值给表对应的持久化类

Query query = session.createQuery("from User as u where list = query.list();
 User usr = (User)list.get(0);

2. 对得到的持久化对象进行修改
 usr.setUsername("Look");
3. 启动事务管理
   Transaction ts = session.beginTransaction();

4. 将赋值后的持久化对象提交给session
  
 session.save(usr);
5. 结束事务管理并向数据库提交
 ts.commit();

-------------------------------
public void update(){
  
  Integer Integer(2);
  Transaction ts = null;
  try {
   Session session = HibernateSessionFactory.currentSession();
   
   Query query = session.createQuery("from User as u where list = query.list();
   User usr = (User)list.get(0);
   usr.setUsername("Look");
   
   ts = session.beginTransaction();
   session.save(usr);
   
   ts.commit();
   
  } catch (HibernateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   try {
    ts.rollback();
   } catch (HibernateException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
  }
 }

Hibernate 的SessionFactory的更多相关文章

  1. Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能

    由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...

  2. JPA EntityManagerFactory Vs Hibernate’s SessionFactory

    JPA使用EntityManagerFactory开闭session,而Hibernate使用SessionFactory开闭session.两者区别: 1. EntityManagerFactory ...

  3. 不同版本Hibernate.获取SessionFactory的方式

    不同版本Hibernate.获取SessionFactory的方式 Hibernate 版本说明: 我当前使用的是 Hibernate 5.x ,(hibernate-release-5.3.6.Fi ...

  4. 8 -- 深入使用Spring -- 8...2 管理Hibernate的SessionFactory

    8.8.2 管理Hibernate的SessionFactory 当通过Hibernate进行持久层访问时,必须先获得SessionFactory对象,它是单个数据库映射关系编译后的内存镜像.在大部分 ...

  5. hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...

  6. hibernate - Initial SessionFactory creation failed.org.hibernate.HibernateException

    对于数据库字段映射, 被映射的字段除了提供 getter方法之外, 还需要提供setter方法. 这也是java bean的一些规范. 例如, 题目中的异常 Initial SessionFactor ...

  7. springboot 获取hibernate 的 SessionFactory

    注入bean package cn.xiaojf; import cn.xiaojf.today.data.rdb.repository.RdbCommonRepositoryImpl; import ...

  8. 浅谈hibernate的sessionFactory和session

    一.hibernate是什么? Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hiber ...

  9. 十八、springboot中hibernate配置sessionFactory访问数据库

    前提 在yml或properties文件中配置数据库与数据库连接池 Hibernate配置 几种方式: 方式一: @Configuration public class HibernateConfig ...

  10. spring配置hibernate的sessionFactory

    1.首先通过dataSource来配置sessionFactory <!--读入配置文件 --> <bean id="propertyConfigurer" cl ...

随机推荐

  1. 使用windows函数SetWindowsHookEx实现键盘钩子

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  2. 2.15 学习总结 之 天气预报APP volley(HTTP库)之StringRequest

    一.说在前面   昨天 学习了序列化的相关知识   今天 1.学习 volley(HTTP库)的 StringRequest请求 2.使用序列化完成相关案例 遇到问题 请求到的参数的出现中文乱码问题 ...

  3. endnote的使用

    下载网址: https://support.clarivate.com/Endnote/s/article/EndNote-Installer-download?language=en_US 关联of ...

  4. css 让多出的文字成省略号...

    一,单行 white-space:nowrap; overflow:hidden;text-overflow: ellipsis; 二,多行 display: -webkit-box; overflo ...

  5. STM32CubeIDE 编译C/C++程序

    文章转自  https://www.cnblogs.com/skyofbitbit/p/3708216.html STM32CubeIDE 其实就是STM32CubeMx + eclipse 首先,W ...

  6. 基于线程池、消息队列和epoll模型实现并发服务器架构

    引言 并发是什么?企业在进行产品开发过程中为什么需要考虑这个问题?想象一下天猫的双11和京东的618活动,一秒的点击量就有几十万甚至上百万,这么多请求一下子涌入到服务器,服务器需要对这么多的请求逐个进 ...

  7. 云时代架构阅读笔记十一——数据库SQL优化

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 ...

  8. linux 常用文件命令记录

    服务开启命令 service  服务  start/stop/stauts 查看ip ifconfig 清屏 clear 显示当前所在位置 pwd 切换目录 cd 查看所有文件(包括隐藏) ls -a ...

  9. 038、Java中逻辑运算之非运算“!”

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  10. 014.Delphi插件之QPlugins,MDI窗口

    不知道为什么,这个DEMO编译出来报错,运行不了,在QDAC群里问了一下也没人响应. 效果如下 主程序代码如下 unit Frm_Main; interface uses Winapi.Windows ...