单表的操作
实体User
1using System;
2
3namespace NHibernate.Examples.QuickStart
4{
5 /**//// <summary>
6 /// Summary description for User.
7 /// </summary>
8 public class User
9 {
10 private string id;
11 private string userName;
12 private string password;
13 private string emailAddress;
14 private DateTime lastLogon;
15
16
17 public User()
18 {
19 }
20
21 public string Id
22 {
23 get { return id; }
24 set { id = value; }
25 }
26
27 public string UserName
28 {
29 get { return userName; }
30 set { userName = value; }
31 }
32
33 public string Password
34 {
35 get { return password; }
36 set { password = value; }
37 }
38
39 public string EmailAddress
40 {
41 get { return emailAddress; }
42 set { emailAddress = value; }
43 }
44
45 public DateTime LastLogon
46 {
47 get { return lastLogon; }
48 set { lastLogon = value; }
49 }
50
51 }
52}
1using System;
2
3namespace NHibernate.Examples.QuickStart
4{
5 /**//// <summary>
6 /// Summary description for User.
7 /// </summary>
8 public class User
9 {
10 private string id;
11 private string userName;
12 private string password;
13 private string emailAddress;
14 private DateTime lastLogon;
15
16
17 public User()
18 {
19 }
20
21 public string Id
22 {
23 get { return id; }
24 set { id = value; }
25 }
26
27 public string UserName
28 {
29 get { return userName; }
30 set { userName = value; }
31 }
32
33 public string Password
34 {
35 get { return password; }
36 set { password = value; }
37 }
38
39 public string EmailAddress
40 {
41 get { return emailAddress; }
42 set { emailAddress = value; }
43 }
44
45 public DateTime LastLogon
46 {
47 get { return lastLogon; }
48 set { lastLogon = value; }
49 }
50
51 }
52}
XML文件
1<?xml version="1.0" encoding="utf-8" ?>
2
3<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
4
5 <class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples" table="users"
6 lazy="false">
7 <id name="Id" column="LogonId" type="String" length="20">
8 <generator class="assigned" />
9 </id>
10
11 <property name="UserName" column="Name" type="String" length="40"/>
12 <property name="Password" type="String" length="20"/>
13 <property name="EmailAddress" type="String" length="40"/>
14 <property name="LastLogon" type="DateTime"/>
15 </class>
16
17</hibernate-mapping>
1<?xml version="1.0" encoding="utf-8" ?>
2
3<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
4
5 <class name="NHibernate.Examples.QuickStart.User, NHibernate.Examples" table="users"
6 lazy="false">
7 <id name="Id" column="LogonId" type="String" length="20">
8 <generator class="assigned" />
9 </id>
10
11 <property name="UserName" column="Name" type="String" length="40"/>
12 <property name="Password" type="String" length="20"/>
13 <property name="EmailAddress" type="String" length="40"/>
14 <property name="LastLogon" type="DateTime"/>
15 </class>
16
17</hibernate-mapping>
简单应用
1using System;
2using System.Collections;
3
4using NHibernate.Cfg;
5
6using NUnit.Framework;
7
8namespace NHibernate.Examples.QuickStart
9{
10 /**//// <summary>
11 /// Summary description for UserFixture.
12 /// </summary>
13 [TestFixture]
14 public class UserFixture
15 {
16 [Test]
17 public void ValidateQuickStart()
18 {
19 Configuration cfg = new Configuration();
20 cfg.AddAssembly("NHibernate.Examples");
21
22 ISessionFactory factory = cfg.BuildSessionFactory();
23 ISession session = factory.OpenSession();
24 ITransaction transaction = session.BeginTransaction();
25
26 User newUser = new User();
27 newUser.Id = "joe_cool";
28 newUser.UserName = "Joseph Cool";
29 newUser.Password = "abc123";
30 newUser.EmailAddress = "joe@cool.com";
31 newUser.LastLogon = DateTime.Now;
32
33 // Tell NHibernate that this object should be saved
34 session.Save(newUser);
35
36 // commit all of the changes to the DB and close the ISession
37 transaction.Commit();
38 session.Close();
39
40 // open another session to retrieve the just inserted user
41 session = factory.OpenSession();
42
43 User joeCool = (User)session.Load(typeof(User), "joe_cool");
44
45 // set Joe Cool's Last Login property
46 joeCool.LastLogon = DateTime.Now;
47
48 // flush the changes from the Session to the Database
49 session.Flush();
50
51 IList recentUsers = session.CreateCriteria(typeof(User))
52 .Add(Expression.Expression.Gt("LastLogon", new DateTime(2004, 03, 14, 20, 0, 0)))
53 .List();
54
55 foreach(User user in recentUsers)
56 {
57 Assert.IsTrue(user.LastLogon > (new DateTime(2004, 03, 14, 20, 0, 0)) );
58 }
59
60 session.Close();
61 }
62 }
63}
1using System;
2using System.Collections;
3
4using NHibernate.Cfg;
5
6using NUnit.Framework;
7
8namespace NHibernate.Examples.QuickStart
9{
10 /**//// <summary>
11 /// Summary description for UserFixture.
12 /// </summary>
13 [TestFixture]
14 public class UserFixture
15 {
16 [Test]
17 public void ValidateQuickStart()
18 {
19 Configuration cfg = new Configuration();
20 cfg.AddAssembly("NHibernate.Examples");
21
22 ISessionFactory factory = cfg.BuildSessionFactory();
23 ISession session = factory.OpenSession();
24 ITransaction transaction = session.BeginTransaction();
25
26 User newUser = new User();
27 newUser.Id = "joe_cool";
28 newUser.UserName = "Joseph Cool";
29 newUser.Password = "abc123";
30 newUser.EmailAddress = "joe@cool.com";
31 newUser.LastLogon = DateTime.Now;
32
33 // Tell NHibernate that this object should be saved
34 session.Save(newUser);
35
36 // commit all of the changes to the DB and close the ISession
37 transaction.Commit();
38 session.Close();
39
40 // open another session to retrieve the just inserted user
41 session = factory.OpenSession();
42
43 User joeCool = (User)session.Load(typeof(User), "joe_cool");
44
45 // set Joe Cool's Last Login property
46 joeCool.LastLogon = DateTime.Now;
47
48 // flush the changes from the Session to the Database
49 session.Flush();
50
51 IList recentUsers = session.CreateCriteria(typeof(User))
52 .Add(Expression.Expression.Gt("LastLogon", new DateTime(2004, 03, 14, 20, 0, 0)))
53 .List();
54
55 foreach(User user in recentUsers)
56 {
57 Assert.IsTrue(user.LastLogon > (new DateTime(2004, 03, 14, 20, 0, 0)) );
58 }
59
60 session.Close();
61 }
62 }
63}
文件里面有。没事放上来。回家看。