初学NHibernate1.2.0.4000注意事项

本来刚刚初学,遇到一些问题罗列一下,给我一样的初学者参考一下。
请先下载NHibernate源码,并添加Application1项目(控制台应用程序)
第一步 建表(SQL语句)
use dbtemp
go

CREATE TABLE users (
  LogonID 
nvarchar(20NOT NULL default '0',
  Name 
nvarchar(40default NULL,
  Password 
nvarchar(20default NULL,
  EmailAddress 
nvarchar(40default NULL,
  LastLogon 
datetime default NULL,
  
PRIMARY KEY  (LogonID)
)
go

第二步 建实体类User.cs
using 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; }
        }

    }
}

第三步写映射文件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>

第四步建配置文件App.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"
    
/>
  
</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=tempdb;Integrated Security=SSPI"
    
/>
  
</nhibernate>
</configuration>

最后一步就可以写运行的Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Cfg;
using NHibernate;

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();

        }
    }
}

这样你就可以运行了。
参考http://www.hibernate.org/362.html
在这里的示例代码你要注意几点:
1.User.hbm.xml这个文件是嵌入的资源的
2.User.hbm.xml文件中
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">要改成<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
3.User.cs文件中前面要加virtual修饰

posted on 2007-08-24 14:24  逐迹工作室  阅读(511)  评论(0编辑  收藏  举报

导航