In Article 1ArticleThere are a few issues as the first Nhibernate getting started demo! I want to write some additional knowledge today! Read:Introduction and demo of nhib.pdf
The following are the upgrades of our project:
Let's take a look.ProgramStructure:
Question 1: About the hibernate. cfg. xml configuration file.
The file name must be hibernate. cfg. xml. . Nhib.pdf automatically finds the file in the project output. You must set the property of this file to always copy.
Problem 2: Configure nhib.pdf in webconfig instead of using hibernate. cfg. xml.
Configuring Nhibernate in webconfig is another configuration method. The format is as follows:
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
<! -- Add this element -->
< Configsections >
< Section
Name = "Hibernate-configuration"
Type = "Nhibernate. cfg. configurationsectionhandler, nhibler"
/>
</ Configsections >
<! -- Add this element -->
< Hibernate-Configuration Xmlns = "Urn: nhibernate-configuration-2.2" >
< Session-factory >
< Property Name = "Dialect" > Nhib.pdf. dialect. mssql2005dialect </ Property >
< Property Name = "Connection. provider" > Nhib.pdf. Connection. driverconnectionprovider </ Property >
< Property Name = "Connection. connection_string" >
Server = (local); initial catalog = hktemp; Integrated Security = sspi
</ Property >
< Mapping Assembly = "Nhibernatedemo" />
</ Session-factory >
</ Hibernate-Configuration >
<! -- Leave the system. web section unchanged -->
< System. Web >
...
</ System. Web >
</ Configuration >
Explanation: nhibect distinguishes us through dialect by using Microsoft SQL Server 2005 database and connects to the database through the specified connection string.
Question 3: sessionfactory is for a database, so we can implement a nhibernatehelper in single-instance mode. See the following:Code[This is the official implementation of nhibernatehelper]
Code
Public Sealed Class Nhibernatehelper
{
Private Const String Currentsessionkey = " Nhibernate. currentsession " ;
Private Static Readonly Isessionfactory sessionfactory;
Static Nhibernatehelper ()
{
Configuration cfg = New Configuration ();
Sessionfactory = New Configuration (). Configure (). buildsessionfactory ();
}
Public Static Isession getcurrentsession ()
{
Httpcontext Context = Httpcontext. Current;
Isession currentsession = Context. items [currentsessionkey] As Isession;
If (Currentsession = Null )
{
Currentsession = Sessionfactory. opensession ();
Context. items [currentsessionkey] = Currentsession;
}
Return Currentsession;
}
Public Static Void Closesession ()
{
Httpcontext Context = Httpcontext. Current;
Isession currentsession = Context. items [currentsessionkey] As Isession;
If (Currentsession = Null )
{
// No current session
Return ;
}
Currentsession. Close ();
Context. Items. Remove (currentsessionkey );
}
Public Static Void Closesessionfactory ()
{
If (Sessionfactory ! = Null )
{
Sessionfactory. Close ();
}
}
}
The above does not report an error in nhibernatehelper: You have correctly configured Nhibernate in webconfig or added the hibernate. cfg. xml configuration file. Note that the file name must be]
After this implementation, our calling code becomes much simpler. Let's take a look at the code.
Isession session=Nhibernatehelper. getcurrentsession ();
//... User Initialization
Session. Save (User );
Session. Delete (User );
Session. Update (User );
The above is a simple upgrade of this demo!
Source code download: NhibernateDemo-2.0.zip