欢迎光临
我们一直在努力

Hibernate配置文件在单元测试中的应用-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

  hibernate 是一个流行的开源对象关系映射工具,单元测试和持续集成的重要性也得到了广泛的推广和认同,在采用了hibernate的项目中如何保证测试的自动化和持续性呢?本文讨论了hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml的过程,以及怎么样将hibernate提供的配置文件的访问方法灵活运用到单元测试中。  介绍  hibernate 是一个流行的开源对象关系映射工具,单元测试和持续集成的重要性也得到了广泛的推广和认同,在采用了hibernate的项目中如何保证测试的自动化和持续性呢?本文讨论了hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml的过程,以及怎么样将hibernate提供的配置文件的访问方法灵活运用到单元测试中。注意:本文以hibernate2.1作为讨论的基础,不保证本文的观点适合于其他版本。   读者    java开发人员,要求熟悉junit和掌握hibernate的基础知识  内容  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。 }}  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、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.hsqldialecthibernate.connection.driver_class org.hsqldb.jdbcdriverhibernate.connection.username sahibernate.connection.passwordhibernate.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的默认的加载过程就比较清楚了。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Hibernate配置文件在单元测试中的应用-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章