I have just been a beginner, And I have encountered some problems to list them. I would like to give them a reference for beginners.
Download the NHibernate source code and add the Application1 Project (console application)
Step 1 create a table (SQL statement) use dbtemp
Go
Create table users (
LogonID nvarchar (20) not null default '0 ',
Name nvarchar (40) default NULL,
Password nvarchar (20) default NULL,
EmailAddress nvarchar (40) default NULL,
LastLogon datetime default NULL,
Primary key (LogonID)
)
Go
Step 2: Create the object class User. csusing System;
Using System. Collections. Generic;
Using System. Text;
Namespace Application1
{
Public class User
{
Private string id;
Private string userName;
Private string password;
Private string emailAddress;
Private DateTime lastLogon;
Public User ()
{
}
Public virtual string Id
{
Get {return id ;}
Set {id = value ;}
}
Public virtual string UserName
{
Get {return userName ;}
Set {userName = value ;}
}
Public virtual string Password
{
Get {return password ;}
Set {password = value ;}
}
Public virtual string EmailAddress
{
Get {return emailAddress ;}
Set {emailAddress = value ;}
}
Public virtual DateTime LastLogon
{
Get {return lastLogon ;}
Set {lastLogon = value ;}
}
}
}
Step 3: Write the ing file User. hbm. xml <? Xml version = "1.0" encoding = "UTF-8"?>
<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2">
<Class name = "Application1.User, Application1" table = "users">
<Id name = "Id" column = "LogonId" type = "String" length = "20">
<Generator class = "assigned"/>
</Id>
<Property name = "UserName" column = "Name" type = "String" length = "40"/>
<Property name = "Password" type = "String" length = "20"/>
<Property name = "EmailAddress" type = "String" length = "40"/>
<Property name = "LastLogon" type = "DateTime"/>
</Class>
</Hibernate-mapping>
Step 4: Create the configuration file App. config <? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<Section
Name = "nhib.pdf"
Type = "System. Configuration. NameValueSectionHandler, System, Version = 1.0.5000.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"
/>
</ConfigSections>
<Nhib.pdf>
<Add
Key = "hibernate. connection. provider"
Value = "nhib.pdf. Connection. DriverConnectionProvider"
/>
<Add
Key = "hibernate. dialect"
Value = "nhibect. Dialect. MsSql2000Dialect"
/>
<Add
Key = "hibernate. connection. driver_class"
Value = "nhib.pdf. Driver. SqlClientDriver"
/>
<Add
Key = "hibernate. connection. connection_string"
Value = "Server = localhost; initial catalog = tempdb; Integrated Security = SSPI"
/>
</Nhib.pdf>
</Configuration>
The last step is to write the running Program. cs.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using nhib.pdf. Cfg;
Using nhib.pdf;
Namespace Application1
{
Class Program
{
Static void Main (string [] args)
{
Configuration cfg = new Configuration ();
Cfg. AddAssembly ("Application1 ");
ISessionFactory factory = cfg. BuildSessionFactory ();
ISession session = factory. OpenSession ();
ITransaction transaction = session. BeginTransaction ();
User newUser = new User ();
NewUser. Id = "joe_cool ";
NewUser. UserName = "Joseph Cool ";
NewUser. Password = "abc123 ";
NewUser. EmailAddress = "joe@cool.com ";
NewUser. LastLogon = DateTime. Now;
Session. Save (newUser );
Transaction. Commit ();
Session. Close ();
}
}
}
In this way, you can run.
Reference http://www.hibernate.org/362.html
Note the following points for the sample code:
1. the User. hbm. xml file is an embedded resource.
2. In the User. hbm. xml file
<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.0"> to be changed to 3. Add virtual modification to the User. cs file.