hibernate中Configuration类的作用
问题:我们在获得一个SessionFactory对象的时候经常是写下面这行代码:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
那么这行代码到底有什么作用,Configuration的对象的作用是什么?
要回答上述问题必须首先知道Configuration对象的作用。
Configuration的作用是:An instance of org.hibernate.cfg.Configuration represents an entire set of mapping of an application's java types to an SQL database.
The org.hibernate.cfg.Configuratio is used to build an immutable org.hibernate.SessionFactory.the mappings are compiled from various xml mapping files.
知道Configuration的对象的作用之后,我们完全可以不要配置文件hibernate.cfg.xml以及在里面进行配置。只需要在一个类中进行加载属性和domain对象的映射文件即可。
首先看这个项目的目录结构如图所示:
其中Person是一个domain对象,Person3.hbm.xml是Person对象与表关联的一个配置文件。Test10是一个测试类,该测试类主要是测试在没有用hibernate.cfg.xml的文件下对数据进行更新。
Person类的代码如下:
package com.qls.domain; import java.util.Date; /**
* Created by 秦林森 on 2017/5/21.
*/
public class Person {
private Integer id;
private String name;
private Date enterCampusDate; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getEnterCampusDate() {
return enterCampusDate;
} public void setEnterCampusDate(Date enterCampusDate) {
this.enterCampusDate = enterCampusDate;
}
}
Person3.hbm.xml文件的代码如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.qls.domain">
<class name="Person" table="person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="enterCampusDate" type="timestamp"/>
</class>
</hibernate-mapping>
Test10类的代码如下:
package com.qls.test; import com.qls.domain.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; /**
* Created by ${秦林森} on 2017/5/22.
*/
public class Test10 {
public static void main(String[] args) {
Configuration configuration = new Configuration();
//set database connection.
configuration
.addResource("/com/qls/configurationFile/Person3.hbm.xml")//com前面的斜杠不能省略。一定要写成/com的形式。
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver")
.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:orcl")
.setProperty("hibernate.connection.username", "scott")
.setProperty("hibernate.connection.password", "a123456")
//设置方言属性
.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
.setProperty("hibernate.connection.pool_size", "10");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();//得到会话。
Transaction tx = session.beginTransaction();//开启事务
Person p = session.get(Person.class, 24);
//更新数据。
p.setName("沉鱼");
session.update(p);
tx.commit();
}
}
至于new Configuration().configure()打开源码就可以看到了,他是读取src下的配置文件hibernate.cfg.xml.
hibernate中Configuration类的作用的更多相关文章
- 关于struts2中ActionContext类的作用
关于struts2中ActionContext类的作用有三个: 1.获取三大作用域对象及页面参数 2.是struts标签的上下文对象 3.ThreadLocal内装的就是ActionContext 怎 ...
- 【转载】C#中SqlCommand类的作用以及常用方法
在C#的数据库操作过程中,SqlCommand类一般用于Sqlserver数据库的SQL语句的执行,包括Select语句.Update语句.Delete语句以及SQL存储过程等,SqlCommand的 ...
- 【转载】C#中SqlConnection类的作用以及常用方法
在C#的数据库编程中,SqlConnection类主要用于连接Sqlserver数据库,使用SqlConnection类的实例方法我们可以打开Sqlserver数据库连接以及获取数据完毕后关闭数据库连 ...
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- hibernate中configuration和配置文件笔记
hibernate的核心类和接口 Configuration类 作用:(1)读取hibernate.cfg.xml文件 (2)管理对象关系映射文件<mapping resource=" ...
- hibernate中几个接口作用
1.Configuration 类 Configuration 类负责管理 Hibernate 的配置信息,包括数据库的URL.用户名.密码.JDBC驱动类,数据库Dialect,数据库连接池等,其加 ...
- Hibernate中持久化类与持久化对象
1.JavaBean类 JavaBean类是实体类,必须一下属性,private修饰的成员属性,public修饰的getter与setter访问方法,public修饰的空参构造器,实现Serializ ...
- 三、hibernate中持久化类的使用
hibernate的持久化类 持久化:将内存中的一个对象持久化到数据库中的过程,hibernate就是一个用来进行持久化的框架 持久化类:一个Java对象与数据库中表建立了关系映射,那么这个类在hib ...
- 05.Hibernate常用的接口和类---Configuration类和作用
Configuration作用: 加载Hibernate配置文件,可以获取SessionFactory对象 加载方式: 1.加载配置文件 Configuration configuration = n ...
随机推荐
- php学习【2】
1:运算符 <?php $x=1; echo 1+1;//算术运算符 echo $x+=5;//赋值运算符 echo "<br/>"; echo $x++; ec ...
- php 微信公众号图文消息回复的实现 与access_token
//代码如下 <?phpclass IndexAction extends Action { public function __construct(){ } public function i ...
- Python学习之set集合
set集合以{}保存一组可迭代对象,如列表,字符串,set集合本身.集合内的元素若有重复的,将自动去除重复元素 a=set([1,2,3]) print(a) b=set('hello python' ...
- Teaching Is a Fruitful Way to Learn【教学是一种有效的学习方式】
Teaching Is a Fruitful Way to Learn For thousands of years, people have known that the best way to u ...
- Matplotlib库介绍
pyplot的plot()函数 pyplot的中文显示 pyplot的文本显示 pyplot的子绘图区域
- python基础之列表、元组和字典
列表 列表定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: 1.可存放多个值 2.可修改指定索引位置对应的值,可变 3.按照从左到右的顺序定义列表元素,下标从0开始顺序 ...
- sql中给逗号分隔的查询结果替换单引号
技术交流群:233513714 第一种方法: SELECT * FROM pay_inf_config a WHERE a.id IN ( SELECT REPLACE ( concat('''', ...
- 树&二叉树&哈夫曼树
1.树 需要注意的两点:n(n>=0)表示结点的个数,m表示子树的个数 (1)n>0时,树的根节点是唯一的. (2)m>0时,子树的个数没有限制. 结点的度和树的度 (1)结点的度是 ...
- ADMX Migrator
实用工具特别推荐ADMX MigratorLance Whitney 下载这篇文章的代码: ADMX Migrator (2765KB) 对于那些 使用组策略的人而言,他们自然非常熟悉如何使用管理模板 ...
- 《Cracking the Coding Interview》——第7章:数学和概率论——题目3
2014-03-20 02:05 题目:给定笛卡尔二维平面上两条直线,判断它们是否相交. 解法:相交.重合.平行. 代码: // 7.3 Given two lines on the Cartesia ...