blogjava's web log

blogjava's web log
...

Nhibernate随记

在asp.net中.配置.NHibernate文件 ,
web.xml

   < 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.prepare_sql"  value ="true" />
    
< add  key ="hibernate.cache.provider_class"  value ="NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache" />
    
< add  key ="hibernate.cache.use_query_cache"  value ="true" />
    
< add  key ="relativeExpiration"  value ="30" />
    
< add  key ="hibernate.dialect"  value ="NHibernate.Dialect.MsSql2000Dialect" />
    
< add  key ="hibernate.connection.driver_class"  value =" NHibernate.Driver.MySqlDataDriver" />
    
< add  key ="hibernate.connection.connection_string"  value ="Server=localhost;database=northwind;uid=sa;pwd=" />
    
<!--  add key="hibernate.connection.connection_string" value="Server=localhost;database=mysql;uid=root;pwd="/ -->
  
</ nhibernate >
  


asp.net 中Nhibernate Session 管理..

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ibatis.common;
namespace WebApplication1
{
    
public class NHSessionModule : IHttpModule
    
{
        
public void Init(HttpApplication context)
        
{
            context.BeginRequest 
+= new EventHandler(Context_BeginRequest);
            context.EndRequest 
+= new EventHandler(Context_EndRequest);
        }

        
public void Dispose()
        
{
            
// Nothing here    
        }

        
private void Context_BeginRequest(object sender, EventArgs e)
        
{
           
            
            
if (!HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.ToUpper().EndsWith(".ASPX"))
             
return;
  // 创建Nhibernate Session      HttpContext.Current.Items.Add("nhsession", ss);

        }


        
private void Context_EndRequest(object sender, EventArgs e)
        
{
           
if (!HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.ToUpper().EndsWith(".ASPX"))
               
return;
                      //销毁
          //  INHSessionManager mgr 
= (INHSessionManager)MindDumpContainer.GetInstance().GetObject(typeof(INHSessionManager));
         //   mgr.Drop();
        }

    }

    
}


配置文件
   <httpModules>

        
<add type="WebApplication1.NHSessionModule" name="NHSessionModule"/>
      
</httpModules>

3:

   加载 hbm.xml 实体

    public NHSession()
       {
           cfg = new Configuration();
          
           foreach (string  entityName in 实体 程序集名称)
           {
               cfg.AddAssembly(entityName.Trim());
           }
         
               factory = cfg.BuildSessionFactory();
      
       }


posted on 2006-08-02 10:09 record java and net 阅读(929) 评论(0)  编辑  收藏 所属分类: javadot net相关


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论