Hibernate basics: Configuration

Source: Internet
Author: User

I am preparing for the interview recently, but I have forgotten about hibernate. I have found some knowledge on the Internet and can check it at any time.

Configuration

Configuration is the entry of hibernate. When you create a Configuration instance, a new SettingsFactory is generated, if a SettingsFactory is input in the constructor, The SettingsFactory is assigned to the SettingsFactory of Hibernate and the reset () method is executed.

The most common Configuration method is:

Configuration cfg = new Configuration (). configure ();

A Configuation instance is generated and the configure () method is called.

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

Then, add all System environment variables (System. getProperties () to GLOBAL_PROPERTIES. If the hibernate. cfg. xml file exists, the system will verify the configuration validity of this file. For some unsupported configuration parameters, the system will print the warning information.

The configure () method first accesses <session-factory> and obtains the name attribute of the element. If it is not empty, the configured value will be used to overwrite hibernate. properties hibernate. session_factory _

Here we can see that the configuration information in hibernate. cfg. xml can overwrite the configuration information of hibernate. properties.

Then, the configure () method accesses the sub-elements of <session-factory>. First, it overwrites the corresponding configuration in hibernate. properties using all the <property> element configuration information.

Configure () Then accesses the content of the following elements in sequence.

<Mapping>
<Jcs-class-cache>
<Jcs-collection-cache>
<Collection-cache>
It is essential to configure <mapping> and configure () to access the ing file (hbm. xml) of the defined java object and relational database table. For example:

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

Other Configuration usage:

The configure () method of Configuration also supports parameter-based access. You can specify hbm. the location of the xml file, instead of using hibernate under the default classpath. cfg. xml, for example:

Configuration cfg = new Configuration (). configure ("myexample. xml ");

At the same time, Configuration also provides a series of methods to customize the process of loading the Configuration file of hibernate, so that your application is more flexible, commonly used is the following:

AddProperties (Element)
AddProperties (Properties)
SetProperties (Properties)
SetProperty (String, String)
In addition to using the default hibernate. properties file, you can also provide multiple. properties configuration file. When Hibernate is used, different configuration files are used according to different situations, for example:

Properties properties = Properties. load ("my. properties ");
Configuration config = new Configuration (). setProperties (properties). configure ();
In addition to specifying the. properties file, you can also specify the. hbm. xml file. The following lists several common methods:

AddClass (Class)
AddFile (File)
AddFile (String)
AddURL)
As mentioned above, the configure () method is accessed by hibernate by default. cfg. xml element to load. hbm. xml file. The methods listed above can directly specify hbm. xml file. For example, the addClass () method can directly load the corresponding ing file by specifying the class. hibernate will automatically convert the full name of the provided class (including package) to the file path, as shown in
Net. sf. hibernate. examples. quickstart. User. class
Net/sf/hibernate/examples/quickstart/User. hbm. xml
You can also use the addFile method to directly specify the ing file. For example:

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 ");
Iv. Summary configuration.

Configuration provides the following advantages:

1. there are usually many applications. hbm. xml ing file, if we only want to test one or several Java PO (Persistence Object) during development, we do not need to put all. hbm. xml is loaded to the memory, which can be directly configured through addClass or addFile, which is very flexible.

2. in the process of learning Hibernate, you often need to practice the features provided by Hibernate. Many features need to be modified in the configuration file, to observe the performance of the same Code under different features, you need to manually modify the configuration file. This is too troublesome and error-prone. We can provide multiple configuration files, each configuration file is configured based on the required features, so that different configuration files are passed as parameters when calling the program, in the program code, you can use setProperties and addFile to specify the input configuration file parameters.

3. in unit testing, especially in integration testing, the entire process is automated. We do not need to manually intervene in the testing process. We often need to prepare multiple configuration files for different test cases, at this time, the setProperties and addFile methods are particularly useful. In different test cases, these methods are used to specify the corresponding configuration file, so that automatic testing can be performed to ensure continuity.


Author: Luckey _

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.