Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Configuration in Hibernate

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about what Configuration is in Hibernate. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Three configuration

Configuration is the entrance to hibernate. When you create an instance of Configuration, a new SettingsFactory will be generated. If you pass a SettingsFactory in the construction parameters, the passed SettingsFactory will be assigned to the SettingsFactory of Hibernate and the reset () method will be executed.

The most common way to use Configuration is:

Configuration cfg = new Configuration () .configure ()

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

The configure () method looks for the hibernate.cfg.xml file under classpath by default. If the file is not found, the system prints the following information and throws a HibernateException exception.

/ hibernate.cfg.xml not found

Then all the system environment variables (System.getProperties ()) are also added to the GLOBAL_PROPERTIES. If the hibernate.cfg.xml file exists, the system will also verify the validity of the file configuration, and the system will print a warning message for some configuration parameters that are no longer supported.

The configure () method first accesses the

< session-factory >

And get the name attribute of the element. If it is not empty, the hibernate.session_factory_ of hibernate.properties will be overridden with the value of this configuration

The configuration value of name, from which we can see that the configuration information in hibernate.cfg.xml can override the configuration information of hibernate.properties.

Then the configure () method accesses the

< session-factory>

You will first use all of the

< property>

The configuration information of the element overrides the corresponding configuration in hibernate.properties.

Then configure () accesses the contents of the following elements sequentially

< mapping>

< jcs-class-cache>

< jcs-collection-cache>

< collection-cache>

It is essential and must be configured

< mapping>

Configure () can access the mapping file (hbm.xml) of the java objects and relational database tables that we defined, such as:

User.hbm.xml

From the above analysis, we have a clear understanding of the default loading process of the hibernate configuration files hibernate.properties and hibernate.cfg.xml.

Other uses of Configuration:

The configure () method of Configuration also supports access with parameters, and you can specify the location of the hbm.xml file instead of using the default hibernate.cfg.xml under classpath, 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 configuration files for hibernate to make your application more flexible, commonly used are the following:

AddProperties (Element) addProperties (Properties) setProperties (Properties) setProperty (String, String)

Through the above methods, in addition to using the default hibernate.properties file, you can also provide multiple .properties configuration files, and use different configuration files according to different situations when using Hibernate, 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. Here are a few common methods:

AddClass (Class) addFile (File) addFile (String) addURL (URL)

As we have mentioned earlier, the configure () method defaults to loading the .hbm.xml file we provided by accessing the elements of hibernate.cfg.xml. The methods listed above can directly specify the hbm.xml file. For example, the addClass () method can load the corresponding mapping file directly by specifying 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

Corresponding net/sf/hibernate/examples/quickstart/User.hbm.xml

You can also specify the mapping file directly using the addFile method. 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")

Fourth, summarize configuration.

The benefits of these methods provided by Configuration are as follows:

1. An application often has a lot of .hbm.xml mapping files, and if the development process is just to test one or more Java PO (Persistence Object), it is not necessary to load all .hbm.xml into memory, so it can be configured directly through addClass or addFile, which is very flexible.

2. In the process of learning Hibernate, we often need to practice to understand the various features provided by Hibernate, and many features need to be modified. If you want to observe the performance of the same code under different characteristics, you need to change the configuration file manually, which is too troublesome and error-prone. We can provide multiple configuration files, each of which is configured according to the desired characteristics. In this way, when we call the program, we pass different configuration files as parameters, and the program code uses setProperties and addFile to specify the passed configuration file parameters.

3. In unit testing, especially in integration testing, the whole process is automated, we can not manually interfere with the testing process, often need to prepare multiple configuration files for different test cases, this time setProperties and addFile methods are particularly useful, in different test cases using these methods to specify the corresponding configuration files, so that automated testing can be achieved to ensure continuity.

Thank you for reading! This is the end of this article on "what is Configuration in Hibernate?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report