The three main objects of the ORM advanced Hibernate

Source: Internet
Author: User

Introduction to ORM Advanced ORM

ORM Advanced Hibernate introduction and Framing

The three main objects of the ORM advanced Hibernate


In our last blog, we talked about how to build a hibernate framework, and mention hibernate we have to say that his three main objects, Configuration,sessionfactory, Session. Let's explain the three main objects separately!

Configuration Object

Configuration class is used to manage the information of our profile, through which we can create a configuration instance to manage the corresponding configuration document, but usually we only create one configuration instance.   Create the process configuration cfg =new configuration (). Configure (); We can also specify the path to the hbm.xml file in the Configure () method .

Sessionfactory Object

sessionfactory as the name implies, He was created Session factory that is used to create Session objects that make you feel strange is sessionfactory It 's not lightweight! In fact its designer's intention is to allow it to be shared throughout the application.  Typically, a project usually requires only one sessionfactory, but when your project is working on multiple databases, you must specify a sessionfactory for each database.

sessionfactory in Hibernate  actually plays the role of a buffer that buffers hibernate's automatically generated SQL statements and some other mapping data, and buffers some data that might be reused in the future.

a sessionfactory The instance corresponds to a data storage source, and the application obtains the session instance from the Sessionfactory. Sessionfactory It is thread-safe, which means that the same instance of it can be shared by multiple threads being applied.

sessionfactory caches can be divided into two categories: built-in caches and external caches.

sessionfactory the built-in cache contains hibernate configuration information and mapping meta-data information, but also cache the hibernate automatically generated SQL statements, and so on;

sessionfactory the external cache is a configurable cache plug-in, by default, the Sessionfactory this cache plug-in is not enabled. The external cache can hold a large number of copies of the database data, the external cache physical media can be memory or hard disk

Session ObjectIntroduction to Session Objects

The session interface is one of the most important interfaces for hibernate developers. In hibernate, however, the instantiated session is a lightweight class, and creating and destroying it does not consume a lot of resources. This is really important in the actual project, because in the client program, the session object may be created and destroyed continuously, if the session overhead is too large, it will adversely affect the system. However, it is important to note that the session object is non-thread safe, so in your design it is better to have a thread that creates only one session object.

In the minds of hibernate designers, they regard the session as a middle interface between data connection and transaction management. We can think of the session as a buffer of persistent objects, hibernate detects changes to these persistent objects, and refreshes the database in a timely manner. We sometimes call the session a persistent Layer manager because it contains some persistent layer-related operations, such as storing persistent objects to the database, and getting them from the database. Note that Hibernate's session differs from HttpSession in JSP applications. When we use the term session, we are referring to the session in Hibernate, and we will later refer to the Httpsesion object as the user session.

how to get the Session object

First createsessionfactoryObject,Application Server If you are accessing multiple data sources,you should produce multiplesessionfactory;but just for service and a request,do not create a newSessionfactory,because creatingsessionfactoryneed to consume a lot of resources.and then according tosessionfactoryre-createSessionObjectThere are two ways to create a session object, namely Opensession () and Getcurrentsession (), opensession, which needs to be closed, but getcurrentsession does not need to be closed. He's going to turn it off automatically!

<span style= "FONT-SIZE:18PX;" >session session=sessionfactory.opensession (); Transaction tx;try{    tx=session.begintransaction ();//Start a transaction ...   //Perform transaction   tx.commit (); catch (Exceptione) {//If an exception occurs, undo the transaction if   (tx!=null)       tx.rollback ();   throw e;} finally{  //Whether the transaction executes successfully or not, finally closes the session and puts it in finally to improve security    session.close ();} </span>

We first read a series of configuration files of the database through the configuration, and then through this series of config information, we create a factory sessionfactory to create the database connection, and then through the Sessionfactory We can get the specific link of the database object, the database to operate!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The three main objects of the ORM advanced Hibernate

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.