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> |
| 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。 } } |
| Configuration cfg = new Configuration().configure(); |
| <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.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 |
| <mapping> <jcs-class-cache> <jcs-collection-cache> <collection-cache> |
| <mapping resource="Cat.hbm.xml"/> |
通过以上的分析,我们对hibernate配置文件hibernate.properties和hibernate.cfg.xml的默认的加载过程就比较清楚了。
4、Configuration的其他用法
Configuration的configure ()方法还支持带参数的访问方式,你可以指定hbm.xml文件的位置,而不是使用默认的classpath下面的hibernate.cfg.xml这种方式,例如:
| Configuration cfg = new Configuration().configure("myexample.xml"); |
| addProperties(Element) addProperties(Properties) setProperties(Properties) setProperty(String, String) |
| Properties properties = Properties.load("my.properties"); Configuration config = new Configuration().setProperties(properties).configure(); |
| addClass(Class) addFile(File) addFile(String) addURL(URL) |
| 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"); |
| 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() { //在此编写测试代码 } } |
| 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() { //在此编写测试代码 } } |