The three main objects of the ORM advanced Hibernate

Source: Internet
Author: User

ORM Advanced ORM Simple Introduction

ORM Advanced Hibernate simple 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 configuration files through it. We can manage the corresponding configuration document by creating one configuration instance, but usually we just create a single 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 's factory. To create a session object, you are surprised that Sessionfactory is 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. Then you must specify a sessionfactory for each database.

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

a sessionfactory instance corresponding to a data storage source. 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, at the same time also cached hibernate self-generated SQL statements, etc.;

sessionfactory the external cache is a configurable cache plug-in, by default, the Sessionfactory this cache plug-in is not enabled.

An external cache can hold a large number of copies of database data. External cached physical media can be memory or hard disk

Session Objecta brief introduction to the Session object

The session interface is one of the most important interfaces for hibernate developers. In hibernate, however, the instantiated session is a lightweight class. Creating and destroying it does not consume a lot of resources.

This is really important in real-world projects, because the session object may be created and destroyed continuously in the client program, assuming that the session is too expensive. Will adversely affect the system.

However, it is important to note that the session object is non-thread-safe and therefore in your design. It is best that a thread simply creates a 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 can detect changes to these persistent objects and refresh the database in a timely manner. We sometimes call the session a persistent Layer manager because it includes some of the persistence 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,when application server assumes access to multiple data sources,you should produce multiplesessionfactory;but only for service and a request,do not create a newSessionfactory,because creatingsessionfactoryneed to consume a lot of resources.then according tosessionfactoryre-createSessionObjectThere are two ways to create session objects, each of which is opensession () and Getcurrentsession (), opensession, which needs to be turned off. But Getcurrentsession does not need to be closed. He'll be off on his own initiative!

<span style= "FONT-SIZE:18PX;" >session session=sessionfactory.opensession (); Transaction tx;try{    tx=session.begintransaction ();//Start a transaction ...   //Run transaction   tx.commit (); catch (Exceptione) {//Assuming an exception occurs, revoke the transaction   if (tx!=null)       tx.rollback ();   throw e;} finally{  //Whether the transaction runs 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. Create a factory sessionfactory to create a database connection, and then through sessionfactory we will be able to get detailed links to the database object, to operate on the database!

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.