Application of hibernate configuration file in unit test

Source: Internet
Author: User
Tags relational database table

Hibernate is a popular open-source object relationship ing tool. The importance of unit testing and continuous integration has been widely promoted and recognized, how can we ensure the automation and continuity of testing in hibernate-enabled projects? This article discusses how hibernate loads its configuration file hibernate. properties and hibernate. cfg. XML, and how to flexibly apply the access method of the configuration file provided by hibernate to unit testing.

  Introduction

Hibernate is a popular open-source object relationship ing tool. The importance of unit testing and continuous integration has been widely promoted and recognized, how can we ensure the automation and continuity of testing in hibernate-enabled projects? This article discusses how hibernate loads its configuration file hibernate. properties and hibernate. cfg. XML, and how to flexibly apply the access method of the configuration file provided by hibernate to unit testing. Note: This article uses hibernate2.1 as the basis for discussion and does not guarantee that this article is applicable to other versions.

  Reader
 
Java developers should be familiar with JUnit and have basic knowledge of hibernate.

  Content

1. Prepare

For beginners of hibernate, the first experience of using Hibernate is usually:

1) install and configure hibernate. We will reference % hibernate_home % later as a reference to the hibernate installation directory,

2) start to create your first example, such as Cat In The Hibernate manual,

3) configure the HBM ing file (such as CAT. HBM. XML, which does not discuss the meaning of configuration items in this file) and the database (such as HSQLDB ),

4) Add a hibernate. cfg. xml file under the classpath path of the project, as shown in the following figure (the most common configuration content of Hibernate is used for the first time ):

<? 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) Then you also need to provide a class to test the creation, update, deletion, and query of Cat. for developers familiar with JUnit, you can create a unit test class for testing, as shown below:

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;
Private transaction TX;

Protected void setup () throws exception {
Configuration CFG = new configuration (). Configure (); // pay attention to this line, which is the focus of this article.
Session = cfg. buildsessionfactory (). opensession ();
Tx = session. begintransaction ();
}

Protected void teardown () throws exception {
TX. Commit ();
Session. Close ();
}

Public void testcreate (){
// Add the relevant code in this method. This article does not discuss how to use the hibernate API.
}
Public void testupdate (){
// Add the relevant code in this method. This article does not discuss how to use the hibernate API.
}
Public void testdelete (){
// Add the relevant code in this method. This article does not discuss how to use the hibernate API.
}
Public void testquery (){
// Add the relevant code in this method. This article does not discuss how to use the hibernate API.
}
}

2. What have new configuration () done?

For beginners who use hibernate for the first time, the following code is the most common configuration method.

Configuration CFG = new configuration (). Configure ();

Configuration is the entry of hibernate. When creating a configuration instance, Hibernate searches for hibernate in classpath. properties file. If the file exists, the content of the file is loaded into the global_properties instance of properties. If the file does not exist, the information is printed.

Hibernate. properties not found

Then add all system environment variables (system. getproperties () to global_properties (note 1 ). If the hibernate. properties file exists, the system will verify the configuration validity of this file. For some configuration parameters that are not supported, the system will print the warning information.

3. What is configure () doing?

New configuration (). The configure () method is discussed below.

By default, the configure () method searches for the hibernate. cfg. xml file under classpath. If the file is not found, the system prints the following information and throws a hibernateexception.

Hibernate. cfg. xml not found

If the file is found, the configure () method first accesses <session-factory> and obtains the name attribute of the element. If it is not empty, the configured value will overwrite hibernate. properties hibernate. the Configuration value of session_factory_name. here we can see that hibernate. cfg. the configuration information in XML can overwrite hibernate. properties configuration information.

Then, the configure () method is used to access the sub-elements of <session-factory>. First, all the <property> element configuration information is used (note 2), as shown in the preceding configuration file.

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

It will overwrite the corresponding configuration in hibernate. properties. The values in the hibernate. properties file (under % hibernate_home %/etc) included in the hibernate2.1 release package are as follows:

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 () Then accesses the content of the following elements in sequence.

<Mapping>
<JCs-class-Cache>
<JCs-collection-Cache>
<Collection-Cache>

<Mapping> is essential. You must configure <mapping> and configure () to access the ing file (HBM) of the defined Java object and relational database table. XML), for example:

<Mapping Resource = "cat. HBM. xml"/>

Through the above analysis, we will be clear about the default loading process of the hibernate configuration file hibernate. properties and hibernate. cfg. xml.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.