首页 > Web开发 > 详细

hibernate中Configuration类的作用

时间:2017-05-22 14:28:26      阅读:385      评论:0      收藏:0      [点我收藏+]

问题:我们在获得一个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.

 

hibernate中Configuration类的作用

原文:http://www.cnblogs.com/1540340840qls/p/6889158.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!