3.1.准备 对于hibernate的初学者来说,第一次使用hibernate的经验通常是: 1, 安装配置好Hibernate,我们后面将%HIBERNATE_HOME%作为对Hibernate安装目录的引用, 2, 开始创建好自己的第一个例子,例如hibernate手册里面的类Cat, 3, 配置好hbm映射文件(例如Cat.hbm.xml,本文不讨论这个文件内配置项的含义)和数据库(如hsqldb), 4, 在项目的classpath路径下添加一个hibernate.cfg.xml文件,如下(第一次使用hibernate最常见的配置内容): <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property> <property name="hibernate.show_sql">false</property> <mapping resource="Cat.hbm.xml"/> </session-factory> </hibernate-configuration> | 5, 然后还需要提供一个类来测试一下创建,更新,删除和查询Cat,对于熟悉JUnit的开发人员,可以创建一个单元测试类来进行测试,如下: import junit.framework.TestCase; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; public class CatTest extends TestCase { private Session session; private Transaction tx; protected void setUp() throws Exception { Configuration cfg = new Configuration().configure();////注意这一行,这是本文重点讨论研究的地方。 session = cfg.buildSessionFactory().openSession(); tx = session.beginTransaction(); } protected void tearDown() throws Exception { tx.commit(); session.close(); } public void testCreate() { //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。 } public void testUpdate() { //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。 } public void testDelete() { //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。 } public void testQuery() { //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。 } } | 3.2 new Configuration()都做了什么? 对于第一次使用hibernate的新手来说,下面的这段代码可以说是最常见的使用Configuration方式。 Configuration cfg = new Configuration().configure(); | Configuration是hibernate的入 口,在新建一个Configuration的实例的时候,hibernate会在classpath里面查找hibernate.properties文 件,如果该文件存在,则将该文件的内容加载到一个Properties的实例GLOBAL_PROPERTIES里面,如果不存在,将打印信息 hibernate.properties not found | 然后是将所有系统环境变量(System.getProperties())也添加到GLOBAL_PROPERTIES里面( 注1)。如果hibernate.properties文件存在,系统还会验证一下这个文件配置的有效性,对于一些已经不支持的配置参数,系统将打印警告信息。 3.3. configure()在做什么? new Configuration()讨论至此,下面讨论configure()方法。 configure()方法默认会在classpath下面寻找hibernate.cfg.xml文件,如果没有找到该文件,系统会打印如下信息并抛出HibernateException异常。 hibernate.cfg.xml not found | 如果找到该文件,configure()方法会首先访 问< session-factory >,并获取该元素的name属性,如果非空,将用这个配置的值来覆盖hibernate.properties的 hibernate.session_factory_name的配置的值,从这里我们可以看出,hibernate.cfg.xml里面的配置信息可以 覆盖hibernate.properties的配置信息。 接着configure()方法访问<session-factory>的子元素,首先将使用所有的<property>元素配置的信息( 注2),如前面我们使用的配置文件 <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.username">sa</property> <property name="connection.password"></property> <property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property> | 会覆盖hibernate.properties里面对应的配置,hibernate2.1发布包里面自带的hibernate.properties文件(位于%HIBERNATE_HOME%/etc下面)里面的值,如下: hibernate.dialect net.sf.hibernate.dialect.HSQLDialect hibernate.connection.driver_class org.hsqldb.jdbcDriver hibernate.connection.username sa hibernate.connection.password hibernate.connection.url jdbc:hsqldb:hsql://localhost | 然后configure()会顺序访问以下几个元素的内容 <mapping> <jcs-class-cache> <jcs-collection-cache> <collection-cache> | 其中<mapping>是必不可少的,必须通过配置<mapping>,configure()才能访问到我们定义的java对象和关系数据库表的映射文件(hbm.xml),例如: <mapping resource="Cat.hbm.xml"/> | 通过以上的分析,我们对hibernate配置文件hibernate.properties和hibernate.cfg.xml的默认的加载过程就比较清楚了。 3.4 Configuration的其他用法 Configuration的configure ()方法还支持带参数的访问方式,你可以指定hbm.xml文件的位置,而不是使用默认的classpath下面的hibernate.cfg.xml这种方式,例如: Configuration cfg = new Configuration().configure("myexample.xml"); | 同时Configuration还提供了一系列方法用来定制hibernate的加载配置文件的过程,让你的应用更加灵活,常用的是以下几种: addProperties(Element) addProperties(Properties) setProperties(Properties) setProperty(String, String) | 通过以上几个方法,除了使用默认的hibernate.properties文件,你还可以提供多个.properties配置文件,使用Hibernate的时候根据不同的情况使用不同的配置文件,例如: Properties properties = Properties.load("my.properties"); Configuration config = new Configuration().setProperties(properties).configure(); | 除了指定.properties文件之外,还可以指定.hbm.xml文件,下面列出几个常用的方法: addClass(Class) addFile(File) addFile(String) addURL(URL) | 前面我们已经讲了,configure()方法默认是 通过访问hibernate.cfg.xml的<mapping>元素来加载我们提供的.hbm.xml文件,上面列出的方法可以直接指定 hbm.xml文件,例如addClass()方法可以直接通过指定class来加载对应的映射文件,hibernate会将提供的class的全名(包 括package)自动转化为文件路径,如net.sf.hibernate.examples.quickstart.Cat.class对应了 net/sf/hibernate/examples/quickstart/Cat.hbm.xml,还可以用addFile方法直接指定映射文件。 例一: Configuration config = new Configuration().addClass(Cat.class); | 例二: Configuration config = new Configuration().addURL(Configuration.class.getResource ("Cat.hbm.xml")); | 例三: Configuration config = new Configuration().addFile("Cat.hbm.xml"); | 3.5 总结 Configuration提供的这些方法的好处如下: 1. 一个应用中往往有很多.hbm.xml映射文件,开发的过程中如果只是为了测试某个或几个Java PO(Persistence Object),我们没有必要把所有的.hbm.xml都加载到内存,这样可以通过addClass或者addFile直接,显得非常灵活。 2. 学习Hibernate的过程中,往往需要通过练习来体会Hibernate提供的各种特征,而很多特征是需要修改配置文件的,如果要观察相同的代码在不 同的特征下的表现,就需要手工改配置文件,这样太麻烦了,而且容易出错,我们可以提供多个配置文件,每个配置文件针对需要的特征而配置,这样我们在调用程 序的时候,把不同的配置文件作为参数传递进去,而程序代码里面使用setProperties和addFile指定传入的配置文件参数就可以了。 3. 在单元测试中,特别是在集成测试里面,整个过程是自动化的,我们不能手工干预测试过程,往往需要准备多个配置文件针对不同的测试案例,这个时候 setProperties和addFile方法就显得特别有用了,在不同的测试案例中用这些方法来指定相应的配置文件,这样就可以做到自动化测试,保证 了持续性。 3.6 应用举例 在 刚开始学习hibernate的时候,对于hibernate的hbm映射文件里的各种配置参数没有一个感性的认识,例如 inverse="true",lazy="true"这样的配置参数,不通过实践是无法体会到其作用的,传统的方法就是每需要测试一种参数的效果就更改 相应的配置文件,然后运行测试来观察结果,如果能够灵活的运用Configuration提供的定制配置的方法,我们可以提供多个配置文件,每个配置文件 里面有不同的配置参数,配合相应的测试案例就方便多了。 例如针对ono-to-many和many-to-one的双向关 联的映射关系,我们想测试在one-to-many一方使用inverse="false"和inverse="true"的不同效果,假设已经正确的配 置好了hibernate.properties,那么还需要提供两个不同的hbm.xml文件,假设分别名为 bidirect.inverse.false.hbm.xml和bidirect.inverse.true.hbm.xml。 然后需要写两个不同的测试案例,分别针对两个不同的配置文件进行测试就可以了,这样的好处是,不用针对不同的测试案例修改配置文件,特别是在集成测试的时候,一切都是自动化的,如果每测试一个案例就需要手工去更改配置文件,这肯定是一个失败的测试。 代码模板如下: FalseInverseTest.java文件 import junit.framework.TestCase; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; /** * test false inverse */ public class FalseInverseTest extends TestCase { private Session session; private Transaction tx; protected void setUp() throws Exception { Configuration cfg = new Configuration().addFile("bidirect.inverse.false.hbm.xml"); session = cfg.buildSessionFactory().openSession(); tx = session.beginTransaction(); } protected void tearDown() throws Exception { tx.commit(); session.close(); } public void testLogic() { //在此编写测试代码 } } | TrueInverseTest.java文件 import junit.framework.TestCase; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; /** * test true inverse */ public class TrueInverseTest extends TestCase { private Session session; private Transaction tx; protected void setUp() throws Exception { Configuration cfg = new Configuration().addFile("bidirect.inverse.true.hbm.xml"); session = cfg.buildSessionFactory().openSession(); tx = session.beginTransaction(); } protected void tearDown() throws Exception { tx.commit(); session.close(); } public void testLogic() { //在此编写测试代码 } } |
结束语 通过对Hibernate默认的配置文件的加载顺序和Hibernate提供的加载配置文件的方法的讨论,我们对在使用到Hibernate的项目的单元测试中使用多个Hibernate配置文件有了比较清楚的认识。 持 续集成中的测试的特征是自动化和持续性,不能手工干预其过程,在使用到Hibernate的项目如果要实现持续集成,就要为不同的测试案例提供不同的配置 文件,而不是针对不同的测试案例进行手工调整,因此,在使用到Hibernate的项目中灵活的运用多配置文件,可以提高测试的效率,保证自动化和持续 性。 注1:有关的代码请参考Environment类的static{}。 注2:如果在hibernate.cfg.xml 的<property/>配置的name没有以hibernate开头,那么configure()内部会自动在前面添加 hibernate,例如connection.url,hibernate会自动将其转化为hibernate.connection.url。 |