Hibernate 的SessionFactory
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的更多相关文章
- Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能
由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...
- JPA EntityManagerFactory Vs Hibernate’s SessionFactory
JPA使用EntityManagerFactory开闭session,而Hibernate使用SessionFactory开闭session.两者区别: 1. EntityManagerFactory ...
- 不同版本Hibernate.获取SessionFactory的方式
不同版本Hibernate.获取SessionFactory的方式 Hibernate 版本说明: 我当前使用的是 Hibernate 5.x ,(hibernate-release-5.3.6.Fi ...
- 8 -- 深入使用Spring -- 8...2 管理Hibernate的SessionFactory
8.8.2 管理Hibernate的SessionFactory 当通过Hibernate进行持久层访问时,必须先获得SessionFactory对象,它是单个数据库映射关系编译后的内存镜像.在大部分 ...
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- hibernate - Initial SessionFactory creation failed.org.hibernate.HibernateException
对于数据库字段映射, 被映射的字段除了提供 getter方法之外, 还需要提供setter方法. 这也是java bean的一些规范. 例如, 题目中的异常 Initial SessionFactor ...
- springboot 获取hibernate 的 SessionFactory
注入bean package cn.xiaojf; import cn.xiaojf.today.data.rdb.repository.RdbCommonRepositoryImpl; import ...
- 浅谈hibernate的sessionFactory和session
一.hibernate是什么? Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hiber ...
- 十八、springboot中hibernate配置sessionFactory访问数据库
前提 在yml或properties文件中配置数据库与数据库连接池 Hibernate配置 几种方式: 方式一: @Configuration public class HibernateConfig ...
- spring配置hibernate的sessionFactory
1.首先通过dataSource来配置sessionFactory <!--读入配置文件 --> <bean id="propertyConfigurer" cl ...
随机推荐
- php+ajax实现无刷新动态加载数据技术
我们浏览有些网页的时候,当拉动浏览器的滚动条时到页底时,页面会继续自动加载更多内容供用户浏览.这种技术我暂且称它为滚屏加载技术.我们发现很多网站用到这种技术,必应图片搜索.新浪微博.QQ空间等将该技术 ...
- C# 篇基础知识8——正则表达式
正则表达式(Regular Expression)也叫匹配模式(Pattern),用来检验字符串是否满足特定规则,或从字符串中捕获满足特定规则的子串.正则表达式的命名空间是System.Text.Re ...
- Linux下如何拷贝整个目录下的所有文件
分类: Linux使用2014-01-14 13:38 1449人阅读 评论(0) 收藏 举报 如何在Linux下拷贝一个目录呢?这好像是再如意不过的问题了.比如要把/home/usera拷贝到/mn ...
- linux问题故障
分析问题的方法论 What-现象是什么样的 When-什么时候发生 Why-为什么会发生 Where-哪个地方发生的问 How much-耗费了多少资源 How to do-怎么解决问题 4. cpu ...
- 数据归一化Scaler-机器学习算法
//2019.08.03下午#机器学习算法的数据归一化(feature scaling)1.数据归一化的必要性:对于机器学习算法的基础训练数据,由于数据类型的不同,其单位及其量纲也是不一样的,而也正是 ...
- git log format
默认git log 出来的格式并不是特别直观,很多时候想要更简便的输出更多或者更少的信息,这里列出几个git log的format. 可以根据自己的需要定制. git log命令可一接受一个--pre ...
- 2.Jsoup
public static void main(String[] args) { //爬取最大资源网上的数据 //用CSS选择器 try { Document doc = Jsoup.parse(ne ...
- [转载]@Component 和 @Bean 的区别
@Component 和 @Bean 的区别 @Component 和 @Bean 的区别 Spring帮助我们管理Bean分为两个部分,一个是注册Bean,一个装配Bean. 完成这两个动作有三种方 ...
- Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project sharp-common: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin
[INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.sharp:sharp-common > ...
- 原生JS写表单验证提交功能
先上效果图: 表单的基础内容就是昵称判断.手机号判断.邮箱判断.身份证号码判断,这里是用到正则验证检验格式. 页面的表单写法就是一个form的提交.输入框用input来实现,输入内容用value来获取 ...