Hibernate Order of execution

Source: Internet
Author: User
Tags commit event listener execution what parameter

1. Instantiate the Configuration object

Hibernate locates the Hibernate.cfg.xml configuration file under Web-inf, instantiating the configuration object based on the information configured in the Hibernate.cfg.xml configuration file. If an event system is used, the event listener should also be registered after the configuration object is instantiated. It is worth mentioning the problem of configuration. Typically, a mapping file is configured for each persisted class, including basic mappings and relational mappings. In Hibernate.cfg.xml, the <mapping resource= "Xxxx.hbm.xml"/> Child elements under the <session-factory/> element are used to list all the persisted class mapping files. Causes configuration to load configuration information for these persisted classes when instantiated.

2. Instantiate the Sessionfactory object

Typically configuration objects invoke the Configure () method, either to get a return value at a time, and the return value calls the Buildsessionfactory () method to instantiate the Sessionfactory object.

3. Instantiate the Session object

With the Sessionfactory object, it can Invoke method Opensession () or opensession (interceptor it) to instantiate a session object. If you use the Opensession () method without using interceptors, use Opensession (Interceptor it) if you use interceptors. The interceptor here is an event framework that implements the class of the Interceptor interface, which, through the interceptor interface, can make a final inspection of the data before it enters the database and, if the data does not meet the requirements, modify the data to avoid illegal data entering the database. In fact, global interceptors can also be enabled through configuration. The event system described in 1 is a more powerful event framework that can be used as an alternative to interceptors, or as a complement to interceptors, as described in the [Reading Summary]hibernate event Framework] article.

4. Start the business

You should set the start point of the transaction before accessing the database, telling the system where to start the operation of the database so that it can be scoped for the system to successfully commit the transaction.

5. Access to the database

The operation of the database does not depend on the data in the database to increase, delete, change, check and other operations. Hibernate's query system is very powerful, including HQL queries, conditional queries, and SQL queries.

Here the hibernate query does not elaborate, only to talk about Hibernate data filtering. Data filtering is not a general method of data query, but a whole filtering method. Data can also be filtered by filtering the data. Filters are very similar to the "WHERE" constraint clause defined in the mapping file on the class and collection, except that the filter can take parameters, and the application can decide at run time whether the given filter is enabled and what parameter values are used. The "where" attribute of the mapping file is in effect and the parameters cannot be passed in dynamically. The usage of a filter is similar to a view, except that an attempt has been made to complete the definition in the database, and the filter also needs to determine the parameter values in the application. Hibernate filters work better than normal query statements. In other words, in the case of using a filter, when the data query, the filter works first, filtered some data and then let the query statement to check.

6. Submission of services

Committing a transaction may or may not be successful. If a commit fails, a rollback is done, and all operations since the start of the transaction are canceled, which reflects the atomicity of the transaction.

7. Close session

Finally, close the session object that you just instantiated.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.