Hibernate is a popular open source object relationship mapping tool, the importance of unit testing and continuous integration has also been extensive promotion and identity, how to ensure the automation and persistence of tests in Hibernate projects? This article discusses the process of Hibernate loading its profile hibernate.properties and hibernate.cfg.xml, and how to use the access method of Hibernate's configuration files to unit tests. Introduction Hibernate is a popular open source object relationship mapping tool, the importance of unit testing and continuous integration has also received extensive promotion and identity, how to ensure the automation and persistence of tests in Hibernate projects? This article discusses the process of Hibernate loading its profile hibernate.properties and hibernate.cfg.xml, and how to use the access method of Hibernate's configuration files to unit tests. Note: This article uses hibernate2.1 as the foundation of the discussion, and does not guarantee the views of this article suitable for other versions. Reader Java developers, ask for familiar with JUnit and master Hibernate's basics 1. Prepare for Hibernate's beginners, the first time I use Hibernate's experience is usually: 1) Installation Hibernate, we will follow% hibernate_home% References to the Hibernate installation directory, 2) Start to create our first example, such as class Cat, 3) in the Hibernate manual, 3) Configure HBM mapping files (such as Cat.hbm.xml, this article does not discuss the configuration item within this file Meaning) and databases (such as HSQLDB), 4) Add a hibernate.cfg.xml file under the ClassPath path of the project, as follows (the first time using Hibernate's most common configuration):
XML Version = "1.0" Encoding = "UTF-8"?>
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 Transaction TX; Protected Void Setup () throws Exception {Configuration CFG = New Configuration (). Configure (); Note this line, this is where this is the focus of discussion research. session = cfg.buildsessionFactory (). OpenSession (); tx = session.begintransAction ();} protected void teardion (); session.close ();}; session.close ();} public void testcreate () Please add a related code in this method. This article does not discuss how to use the Hibernate API. } Public void testUpdate () {// Please add related code in this method, this article does not discuss how to use the Hibernate API. } Public void testdelete () {// Please add related code in this method, this article does not discuss how to use the Hibernate API. } Public void testQuery () {// Please add related code in this method, this article does not discuss how to use the Hibernate API. }} 2, what did New Configuration () do? For newcomers using Hibernate for the first time, the following code can be said to be the most common way of using Configuration.
Configuration cfg = new configuration (). Configure (); configure is the port of Hibernate, when new Configuration instances, Hibernate look up hibernate.properties files in ClassPath, if the file exists, load the contents of the file To a Properties Example Global_Properties, if there is no existence, print information hibernate.properties NOT FOUND is then added to global_properties (Note 1) in Global_Properties (Note 1). If the hibernate.properties file exists, the system also verifies the validity of this file configuration. For some unsupported configuration parameters, the system will print a warning message. 3, what is configure () What is it? New configuration () discussed here, discussed the configure () method. Configure () Method The following is looking for hibernate.cfg.xml files below ClassPath, and if this file is not found, the system will print the following information and throw the HibernateException exception. Hibernate.cfg.xml not found If this file is found, the configure () method will first access