• 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.

  • 相关阅读:
    Redis为什么要自己实现一个SDS
    Redis中的数据结构
    编程题
    设计模式-代理模式
    设计模式-原型模式
    设计模式-工厂模式(简单工厂,工厂方法,抽象工厂)
    Redis基础
    Windows提高_1.4进程通信
    Windows提高_1.3文件操作
    Windows提高_1.2遍历进程、遍历模块
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/6889158.html
Copyright © 2020-2023  润新知