Application of Hibernate configuration file in unit test

xiaoxiao2021-03-05  24

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):

jdbc: hsqldb: hsql: // localhost Org.hsqldb.jdbcdriver net.sf. Hibernate.Dialev.hsqldiaalect false 5 ) Then you need to provide a class to test CAT, you can create a unit test class for testing for developers who are familiar with JUnit, as follows:

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 , and get the Name property of the element. If it is not empty, use the value of this configuration to cover hibernate.properties Hibernate. .Session_Factory_name The value of the configuration of the configuration, from here we can see that configuration information in hibernate.cfg.xml can overwrite configuration information for hibernate.properties. The Configure () method then accesses the child elements of , first use all of the element configuration (Note 2), such as the configuration file we use in front.

jdbc: hsqldb: hsql: // localhost org.hsqldb.jdbcDriver SA net.sf.hibernate.diaalect.hsqldiaalect can overwrite corresponding to Hibernate.properties configuration, the package itself comes Hibernate2.1 hibernate.properties file (in% HIBERNATE_HOME% / etc below) the value of which follows: hibernate.dialect net.sf.hibernate.dialect.HSQLDialecthibernate.connection.driver_class org.hsqldb. JDBCDRIVERHIBERNATE.CONNECTION.USERNAME SAHIBERNATE.CONNECTION.PASSWORDHIBERNATE.CONNECTION.URL JDBC: HSQLDB: HSQL: // localhost then configure () sequentially accesses the content of the following elements

wherein is essential, you must access our defined Java object by configuring , configure () And the mapping file (HBM.XML) of the relational database table, for example:

Through the above analysis, we are more clear about the default load process for Hibernate profile hibernate.properties and hibernate.cfg.xml.

转载请注明原文地址:https://www.9cbs.com/read-35533.html

New Post(0)