hibernate中Configuration类的作用

问题:我们在获得一个SessionFactory对象的时候经常是写下面这行代码:

1   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类的代码如下:

 1 package com.qls.domain;
 2
 3 import java.util.Date;
 4
 5 /**
 6  * Created by 秦林森 on 2017/5/21.
 7  */
 8 public class Person {
 9     private Integer id;
10     private String  name;
11     private Date enterCampusDate;
12
13     public Integer getId() {
14         return id;
15     }
16
17     public void setId(Integer id) {
18         this.id = id;
19     }
20
21     public String getName() {
22         return name;
23     }
24
25     public void setName(String name) {
26         this.name = name;
27     }
28
29     public Date getEnterCampusDate() {
30         return enterCampusDate;
31     }
32
33     public void setEnterCampusDate(Date enterCampusDate) {
34         this.enterCampusDate = enterCampusDate;
35     }
36 }

Person3.hbm.xml文件的代码如下:

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5
 6 <hibernate-mapping package="com.qls.domain">
 7     <class name="Person" table="person">
 8 <id name="id" column="person_id">
 9 <generator class="native"/>
10 </id>
11 <property name="name"/>
12 <property name="enterCampusDate" type="timestamp"/>
13         </class>
14         </hibernate-mapping>

Test10类的代码如下:

 1 package com.qls.test;
 2
 3 import com.qls.domain.Person;
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.hibernate.cfg.Configuration;
 8
 9 /**
10  * Created by ${秦林森} on 2017/5/22.
11  */
12 public class Test10 {
13     public static void main(String[] args) {
14         Configuration configuration = new Configuration();
15         //set database connection.
16         configuration
17                 .addResource("/com/qls/configurationFile/Person3.hbm.xml")//com前面的斜杠不能省略。一定要写成/com的形式。
18                 .setProperty("hibernate.show_sql", "true")
19                 .setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver")
20                 .setProperty("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:orcl")
21                 .setProperty("hibernate.connection.username", "scott")
22                 .setProperty("hibernate.connection.password", "a123456")
23                 //设置方言属性
24                 .setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
25                 .setProperty("hibernate.connection.pool_size", "10");
26         SessionFactory sessionFactory = configuration.buildSessionFactory();
27         Session session = sessionFactory.openSession();//得到会话。
28         Transaction tx = session.beginTransaction();//开启事务
29         Person p = session.get(Person.class, 24);
30         //更新数据。
31         p.setName("沉鱼");
32         session.update(p);
33         tx.commit();
34     }
35 }

至于new Configuration().configure()打开源码就可以看到了,他是读取src下的配置文件hibernate.cfg.xml.

时间: 2024-08-25 08:56:15

hibernate中Configuration类的作用的相关文章

hadoop中Configuration类剖析

Configuration是hadoop中五大组件的公用类,所以放在了core下,org.apache.hadoop.conf.Configruration.这个类是作业的配置信息类,任何作用的配置信息必须通过Configuration传递,因为通过Configuration可以实现在多个mapper和多个reducer任务之间共享信息. 类图 说明:Configuration实现了Iterable和Writable两个接口,其中实现Iterable是为了迭代,迭代出Configuration对

三、hibernate中持久化类的使用

hibernate的持久化类 持久化:将内存中的一个对象持久化到数据库中的过程,hibernate就是一个用来进行持久化的框架 持久化类:一个Java对象与数据库中表建立了关系映射,那么这个类在hibernate中就可以称之为持久化类 Java实体类 该Java类的映射文件 持久化类的使用 提供无参构造 从之前测试类中查询的使用来看: User user = session.get(User.class, 1); 说明hibernate内部是使用反射技术实现生成对象实例,所以持久化类中的Java

Hibernate常用的接口和类---Configuration类和作用

Configuration作用: 加载Hibernate配置文件,可以获取SessionFactory对象 加载方式: 1.加载配置文件 Configuration configuration = new Configuration(); 2.加载映射文件 使用porperties配置文件的方式 configuration.addResource("cn/itcast/domain/Student.hbm.xml"); 使用XML配置文件的方式 config.configure();

Android开发中Context类的作用以及Context的详细用法

Android中Context的作用以及Context的详细用法 本文我们一起来探讨一下关于Android中Context的作用以及Context的详细用法,这对我们学习Android的资源访问有很大的帮助,文章中也贴出了一些关于Android Context使用的示例代码,非常不错,以下是原文: Context基本概念 Context是什么? 1) Context是一个抽象类,其通用实现在ContextImpl类中. 2) Context:是一个访问application环境全局信息的接口,通

hibernate中实体类注解

一.JPA通用策略生成器 通过annotation来映射hibernate实体的,基于annotation的hibernate主键标识为@Id, 其生成规则由@GeneratedValue设定的.这里的@id和@GeneratedValue都是JPA的标准用法, JPA提供四种标准用法,由@GeneratedValue的源代码可以明显看出. Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface GeneratedValue{ G

【转载】Java中StringTokenizer类的作用

StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数.1.构造函数public StringTokenizer(String str)public StringTokenizer(String str, String delim)public StringTokenizer(String str, String delim, boolean returnDelims)第一个参数就是要分隔的String,第二个是分隔字符集合,第三个参数表示分隔符号是否作为标

hibernate中实体类对象的四种状态

1.临时状态(transient):用new语句创建,还没有被持久化,并且不在Session的缓存中. 标识:OID为null,没有和Session建立关系.2.持久化状态(persistent):已经计划被持久化,并且加入到Session的缓存中.(为什么说计划:因为事务问题,是否已经提交事务)标识:OID不为null,建立了和Session的关系.3.删除状态(removed):(可以不管,如果说三种状态,就是没有此种)不在Session的缓存中,且Session已经计划将其从数据库中删除.

在java中RandomAccessFile类的作用:对指定文件可以进行读写的操作

Java中Class和单例类的作用与类成员的理解

Java中Class类的作用与深入理解 在程序运行期间,Java运行时系统始终为所有的对象维护一个被称为运行时的类型标识.这个信息跟踪着每个对象所属的类.JVM利用运行时信息选择相应的方法执行.而保存这些信息的类称为Class.可能容易产生混淆,容易想到class.不过二者没什么关系,class不过是描述类的一个关键字.而Class却是保存着运行时信息的类. 它能做什么?Class类可以帮助我们在程序运行时分析类,说白了就是获取类中的值.可能瞬间就想到了反射,没错!Class一般就是和反射配套使