ArticleDirectory
- 2. 1. Build sessionfactory
- 2.6.1 construct sessionfactory by reading configuration information from XML file
- 2.6.2 construct sessionfactory by reading configuration information from the Java property File
- 3.1session Creation
- 3.2session Closure
References
Original article: http://www.cnblogs.com/liuyang-1037/archive/2009/03/26/1422254.html
1. Configuration:
Manages the configuration information of hibernate, which is from the configuration file hibernate. cfg. XML or hibernate. properties can also customize the file name. You only need to specify the specific path when instantiating the configuration;
2. sessionfactiory:
The configuration instance constructs the sessionfactory instance based on the current configuration information. Sessionfactory is thread-safe. Generally, a database in the next application shares a sessionfactory instance.
2. 1. Build sessionfactory
The sessionfactory interface of hibernate provides an instance of the session class, which is used to complete database operations. Because the sessionfactory instance is thread-safe (and the session instance is not thread-safe), each operation can use the same sessionfactory to obtain the session.
Hibernate configuration files are divided into two formats: xml configuration files and Java attribute file configuration files. Therefore, sessionfactory can be constructed in two ways, the following sections describe each other.
2.6.1 construct sessionfactory by reading configuration information from XML file
The procedure for building sessionfactory by reading configuration information from an XML file is as follows.
(1) create a configuration object and load the hibernate configuration file using the configura () method of the object,CodeAs follows.
Configuration Config =NewConfiguration (). Configure ();
Configura () method: Used to tell hibernate to load the hibernate. cfg. xml file. Configuration loads hibernate in classpath by default during instantiation. cfg. XML. Of course, the name can also be loaded instead of hibernate. cfg. xml configuration file, such as wghhibernate. cfg. XML, which can be implemented using the following code.
Configuration Config =NewConfiguration (). Configure ("wghhibernate. cfg. xml ");
(2) After the configuration file and ing file are loaded, a configuration instance containing all the parameters of the hibernate runtime will be obtained. A unique sessionfactory can be built through the buildsessionfactory () method of the configuration instance, the Code is as follows.
Sessionfactory = config. buildsessionfactory ();
The construction of sessionfactory should be placed in the static code block because it is executed only once when the class is loaded. A typical code for building sessionfactory is as follows.
View code
Import Org. hibernate .* ; Import Org. hibernate. cfg .* ; Public Class Coresession { Static Sessionfactory; // Note that sessionfactory is static here. // Initialize Hibernate and create a sessionfactory instance, which is executed only once when the class is loaded into the memory. Static { Try {Configuration config = New Configuration (). Configure (); sessionfactory = Config. buildsessionfactory ();} Catch (Exception e) {system. Out. println (E. getmessage ());}}}
2.6.2 construct sessionfactory by reading configuration information from the Java property File
The steps for building sessionfactory by reading configuration information from the Java property file are as follows.
(1) create a configuration object. At this time, Hibernate loads the configuration file hibernate. properties in classpath by default. The Code is as follows.
Configuration Config =NewConfiguration ();
(2) due to the lack of information about the corresponding configuration ing file in the configuration file, encoding is required here. This can be achieved through the addclass () method of the configuration object, the Code is as follows.
Config. addclass (branchform.Class);
The addclass () method is used to load object classes.
(3) After the configuration file and ing file are loaded, a configuration instance containing all the parameters of the hibernate runtime will be obtained. A unique sessionfactory can be built through the buildsessionfactory () method of the configuration instance, the Code is as follows.
Sessionfactory = config. buildsessionfactory ();
The construction of sessionfactory should be placed in the static code block, because it only needs to be executed once when the class is loaded. The code for a typical construction of sessionfactory is as follows.
View code
Import Org. hibernate .* ; Import Org. hibernate. cfg .* ; Public Class Coresession { Static Sessionfactory; // Initialize Hibernate and create a sessionfactory instance, which is executed only once when the class is loaded into the memory. Static { Try {Configuration config = New Configuration (); config. addclass (branchform. Class ); Sessionfactory = Config. buildsessionfactory ();} Catch (Exception e) {system. Out. println (E. getmessage ());}}}
3. Session:
The general persistence method (crud) is called through the session, and the session is non-thread-safe. Session creation and closure: a session is a lightweight object. Generally, each session instance is bound to a database transaction, that is, each database transaction is executed, you must first create a new session instance. After using the session, you also need to disable the session.
3.1session Creation
After creating sessionfactory, you can create a session instance through sessionfactory. The code for creating a session instance through sessionfactory is as follows.
Session session = sessionfactory. opensession ();
After creating a session, you can perform the persistence operation through the created session.
3.2session Closure
After creating a session instance, you must close the session instance and release the resources occupied by the session instance no matter whether or not the transaction is executed. The code for disabling a session instance is as follows:
Session. Close ();