Hello NHibernate(Chapter 2 of NHibernate in action)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 namespace HelloWorldNHibernate
2 {
3 class Employee
4 {
5 public int id;
6 public string name;
7 public Employee manager;
8
9 public string SayHello()
10 {
11 return string.Format("Hello from {0}", name);
12 }
13 }
14 }
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using NHibernate;
5 using NHibernate.Cfg;
6 using System.Reflection;
7
8 namespace HelloWorldNHibernate
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 CreateEmployeeAndSaveToDatabase();
15 UpdateRayAndAssignLeonAsManager();
16 LoadEmployeesFromDatabase();
17 Console.Read();
18 }
19
20 static void CreateEmployeeAndSaveToDatabase()
21 {
22 Employee ray = new Employee();
23 ray.name = "Ray Zhang";
24
25 using (ISession session = OpenSession())
26 {
27 using (ITransaction transaction = session.BeginTransaction())
28 {
29 session.Save(ray);
30 transaction.Commit();
31 }
32
33 Console.WriteLine("Saved ray to db");
34 }
35 }
36
37 static ISession OpenSession()
38 {
39 Configuration c = new Configuration();
40 c.AddAssembly(Assembly.GetCallingAssembly());
41 ISessionFactory f = c.BuildSessionFactory();
42 return f.OpenSession();
43 }
44
45 static void LoadEmployeesFromDatabase()
46 {
47 using(ISession session = OpenSession())
48 {
49 IQuery query = session.CreateQuery("from Employee as emp Order by emp.name asc");
50
51 IList<Employee> foundEmployees = query.List<Employee>();
52 Console.WriteLine("\n{0} employees found:", foundEmployees.Count);
53
54 foreach (Employee e in foundEmployees)
55 {
56 Console.WriteLine(e.SayHello());
57 }
58 }
59 }
60
61 static void UpdateRayAndAssignLeonAsManager()
62 {
63 using (ISession session = OpenSession())
64 {
65 using (ITransaction transaction = session.BeginTransaction())
66 {
67 IQuery query = session.CreateQuery("from Employee where name = 'Ray Zhang'");
68 Employee ray = query.List<Employee>()[0];
69 ray.name = "Ray Chang";
70
71 Employee leon = new Employee();
72 leon.name = "Leon Fu";
73
74 ray.manager = leon;
75 session.Flush();
76 transaction.Commit();
77
78 Console.WriteLine("Updated ray and added leon");
79 }
80 }
81 }
82 }
83
84 }
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 <?xml version="1.0" encoding="utf-8" ?>
2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
3 <class name="HelloWorldNHibernate.Employee, HelloWorldNHibernate" table="Employee" lazy="false">
4 <id name="id" access="field">
5 <generator class="native"></generator>
6 </id>
7 <property name="name" access="field" column="name"></property>
8 <many-to-one access="field" name="manager" column="manager" cascade="all"></many-to-one>
9 </class>
10 </hibernate-mapping>
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <configSections>
4 <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
5 </configSections>
6 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
7 <session-factory>
8 <property name="connection.provider">
9 NHibernate.Connection.DriverConnectionProvider
10 </property>
11 <property name="dialect">
12 NHibernate.Dialect.MsSql2005Dialect
13 </property>
14 <property name="connection.driver_class">
15 NHibernate.Driver.SqlClientDriver
16 </property>
17 <property name="connection.connection_string">
18 Server=127.0.0.1;Database=HelloNHibernate;User ID=sa;Password=sbs;Trusted_Connection=True
19 </property>
20 <property name="proxyfactory.factory_class">
21 NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
22 </property>
23
24 </session-factory>
25 </hibernate-configuration>
26 </configuration>
1 CREATE DATABASE HelloNHibernate
2 GO
3
4 USE HelloNHibernate
5 GO
6 CREATE TABLE Employee
7 (
8 id int identity primary key,
9 name varchar(50),
10 manager int
11 )
2 GO
3
4 USE HelloNHibernate
5 GO
6 CREATE TABLE Employee
7 (
8 id int identity primary key,
9 name varchar(50),
10 manager int
11 )