前几天在回答一个问题时,我是凭直感回答的,因为没试过。正好最近对O/R Mapper感兴趣,所以决定试一下NHibernate,发现其Quick Start Guide 有几个地方不是很清楚,现在把试验的步骤记录如下。(因为试验的这台机器没有VS.NET,所以用了csc.exe来直接编译)
}
}
3. 生成影射文件 User.hbm.xml
<?xml version=”1.0″ encoding=”utf-8″ ?>
<hibernate-mapping xmlns=”urn:nhibernate-mapping-2.0″>
<class name=”NHibernate.Examples.QuickStart.User, NHibernate.Examples” 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>
4. 生成配置文件 TestUser.exe.config
<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<configSections>
<section name=”nhibernate” type=”System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null” />
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” />
</configSections>
<nhibernate>
<add
key=”hibernate.connection.provider”
value=”NHibernate.Connection.DriverConnectionProvider”
/>
<add
key=”hibernate.dialect”
value=”NHibernate.Dialect.MsSql2000Dialect”
/>
<add
key=”hibernate.connection.driver_class”
value=”NHibernate.Driver.SqlClientDriver”
/>
<add
key=”hibernate.connection.connection_string”
value=”Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI”
/>
</nhibernate>
<log4net>
<appender name=”ConsoleAppender” type=”log4net.Appender.ConsoleAppender”>
<layout type=”log4net.Layout.PatternLayout”>
<param name=”ConversionPattern” value=”%d [%t] %-5p %c [%x] -%m%n” />
</layout>
<root>
<level value=”INFO” />
<appender-ref ref=”ConsoleAppender” />
</root>
</appender>
</log4net>
</configuration>
- 生成测试程序:TestUser.cs
using System;
//using System.Configurations;
using NHibernate;
using NHibernate.Cfg;
using System.Collections;
using NHibernate.Examples.QuickStart;
using NHibernate.Expression;
class TestConfig
{
public static void Main()
{
Configuration cfg = new Configuration();
cfg.AddAssembly(“NHibernate.Examples”);
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;// Tell NHibernate that this object should be saved
session.Save(newUser);
// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();Console.WriteLine(“go check in the database, enter return to continue….”);
Console.ReadLine();
//System.Threading.Thread.Sleep(2000);
//Console.WriteLine(“enter return to continue….”);
//Console.ReadLine();
// open another session to retrieve the just inserted user
session = factory.OpenSession();
User joeCool = (User)session.Load(typeof(User), “joe_cool”);
// set Joe Cool’s Last Login property
joeCool.LastLogon = DateTime.Now;
session.Update(joeCool);
// flush the changes from the Session to the Database
session.Flush();
//Console.WriteLine(“enter return to continue….”);
//Console.ReadLine();
IList userList = session.CreateCriteria(typeof(User)).List();
foreach(User user in userList)
{
Console.WriteLine(user.Id + ” last logged in at ” + user.LastLogon);
}
IList recentUsers = session.CreateCriteria(typeof(User))
.Add(Expression.Gt(“LastLogon”, new DateTime(2004, 03, 14, 20, 0, 0)))
.List();
foreach(User user in recentUsers)
{
Console.WriteLine(user.Id + ” last logged in at ” + user.LastLogon);
}session.Close();
}
} - copy or reference
NHibernate.dll
HashCodeProvider.dll (needed at runtime)
log4net.dll (needed at runtime) - compile User.cs:
csc /t:library /out:NHibernate.Examples.dll /resource:User.hbm.xml User.cs - compile TestUser.cs:
csc /r:NHibernate.dll,NHibernate.Examples.dll TestUser.cs - run TestUser.exe
F:\test>TestUser
go check in the database, enter return to continue….
joe_cool last logged in at 12/15/2004 9:42:00 PM
joe_cool last logged in at 12/15/2004 9:42:00 PM
注,其中log4net的配置是从原提问者inelm处学来的