Lae's Blog

CODE LIFE --像蚂蚁一样工作,像蝴蝶一样生活

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

配置文件如下  

代码
<?xml version="1.0"?>
<configuration>
    
<configSections>
        
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
        
<section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler,NHibernate.Caches.SysCache" />
    
</configSections>
    
<connectionStrings>
        
<add name="db" connectionString="Server=.;Database=NHTest;Trusted_Connection=SSPI"/>
        
<add name="nhtest" connectionString="Data Source=orcl; Persist Security Info=True;Integrated Security=False;User ID=nhtest;Password=nhtest;"/>
    
</connectionStrings>

    
<!--Oracle-->
    
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        
<session-factory>
            
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
            
<property name="dialect">NHibernate.Dialect.Oracle10gDialect,NHibernate</property>
            
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver,NHibernate</property>
            
<property name="connection.connection_string_name">nhtest</property>
            
<property name="adonet.batch_size">100</property>
            
<property name="show_sql">true</property>
            
<property name="hbm2ddl.auto">update</property>

            
<property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider,NHibernate.Caches.SysCache</property>
            
<!--<property name="cache.use_second_level_cache">true</property>
            
<property name="cache.use_query_cache">true</property>-->

            
<!--<class-cache class="Test.Nh3.Domain, Test.Nh3" region="hourly" usage="read-only"/>-->

        
</session-factory>
    
</hibernate-configuration>
    
<syscache>
        
<cache region="hourly" expiration="60" priority="3" />
    
</syscache>

    
<runtime>
        
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            
<dependentAssembly>
                
<assemblyIdentity name="NHibernate" publicKeyToken="AA95F207798DFDB4" culture="neutral"/>
                
<bindingRedirect oldVersion="0.0.0.0-3.0.0.2002" newVersion="3.0.0.2002"/>
            
</dependentAssembly>
        
</assemblyBinding>
    
</runtime>
</configuration>


 初始化Mapper

代码
 public HbmMapping GetMapping()
        {
            
//初始化ObjectRelationalMapper类
            var orm = new ObjectRelationalMapper();

            
//TablePerClass:每个类一张表映射策略
            
//TablePerClassHierarchy:每个类分层结构一张表映射策略
            
//TablePerConcreteClass:每个具体类一张表映射策略
            orm.TablePerClass<Domain>();
            
//orm.Patterns.Poids.Add(new GuidCombGeneratorDef());
            
//在这里可以调用ObjectRelationalMapper类一些方法配置Domain语义
            
//使用orm参数初始化Mapper类
            var mapper = new Mapper(orm);
            
//在这里可以调用Mapper类一些方法配置Domain的Mapping
            
//调用Mapper类的CompileMappingFor方法编译生成HbmMapping对象
            return mapper.CompileMappingFor(new[] { typeof(Domain) });
           
        }


初始化config

代码
  var nhConfig = new Configuration().Configure();
            nhConfig.AddDeserializedMapping(GetMapping(), 
null);

            
//配置缓存
            nhConfig.SetProperty(NHibernate.Cfg.Environment.UseSecondLevelCache, "true")
                .SetProperty(NHibernate.Cfg.Environment.UseQueryCache, 
"true")
                .Cache(c 
=> c.Provider<SysCacheProvider>())
                .EntityCache
<Domain>(c =>
                {
                    c.Strategy 
= EntityCacheUsage.ReadWrite;
                    c.RegionName 
= "hourly";
                });

            
//配置数据库架构元数据
            
//SchemaMetadataUpdater.QuoteTableAndColumns(nhConfig);
            
//创建数据库架构
            
//new SchemaExport(nhConfig).Create(false, true);

            var factory 
= nhConfig.BuildSessionFactory();

            
using (var s = factory.OpenSession())
            {
                
using (var tx = s.BeginTransaction())
                {
                    var domain 
= new Domain
                    {
                        Name 
= "我的测试" + DateTime.Now.ToString(),
                        Address 
= "中国",
                        Picture
=GetPic()
                    };
          
                    s.Save(domain);
                    tx.Commit();
                }
            }


    尽管配置了缓存,性能问题依然出现在picture保存上面,竟然需要20多秒,期待有人能研究出这个解决办法。

 -------------------------------------------------------------

 构造领域模型时需要使用如下代码,等同于在*.hbm.xml文件将实体列映射的类型显式声明

 var mapper = new Mapper(orm);
            mapper.Class<Domain>(dm =>
            {
                dm.Property(x => x.Picture,
                            m => m.Type(NHibernate.NHibernateUtil.BinaryBlob));
            }); 

 

posted on 2010-11-12 13:27  lae  阅读(470)  评论(0编辑  收藏  举报