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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值