zoukankan      html  css  js  c++  java
  • NHibernate学习总结:(一)NHibernate的使用和配置 Virus

     

    从今天开始学习NHibernate,同时记录一些遇到的问题和学习的经验,希望对大家有一些帮助。

    NHibernate的具体作用之类的,我就不介绍了,直接开始它的使用。

    开发工具:VS2008 SP1

    开发环境:Win2003 、.NET 3.5 SP1、MS SQL Server 2005

     同时我也参考了: 

    NHibernate之旅系列,是一个很好的教程,替作者广告一下,哈哈。

    http://www.cnblogs.com/lyj/archive/2008/10/15/1312089.html

    在数据库中新建如下图的数据库结构,数据库的名称为NHibernate,包括Customer、Order、Product、OrderProduct四张表,其中的Id为int,不是自增列,其他列是varchar(50),Cost为float具体结构如下图

    NHibernate1.1

    1、实体类代码

    public  class Customer
        {
            public virtual int Id { get; set; }
            public virtual string Firstname{get;set;}
            public virtual string Version { get; set; }
            public virtual string Lastname { get; set; }
        }

    2、对应的映射文件Customer.hbm.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample.Domain(实体类程序集的名称)"
                        namespace="NHibernateSample.Domain.Entities(实体类的命名空间)">
      <class name="NHibernateSample.Domain.Entities.Customer(实体类的完全限定名), NHibernateSample.Domain(实体类程序集名称)" table="Customer" lazy="false">
        <id name="Id" column="CustomerId" type="System.Int32">
          <generator class="assigned"></generator>
        </id>
        <property name="Version" column="Version"/>
        <property name="Firstname" column="Firstname"/>
        <property name="Lastname" column="Lastname"/>

      </class>
    </hibernate-mapping>

     

     

    3、web.config中的配置

     

    <?xml version="1.0"?>
    <configuration>
        <configSections>
            <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
        </configSections>
        <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
            <session-factory name="NHibernate.Test">
                <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
                <property name="connection.connection_string">
            Data Source=SHIWB\SQL2005DEV;Database=NHibernate;User ID=as;Password=123456;
          </property>
                <property name="adonet.batch_size">10</property>
                <property name="show_sql">false</property>
                <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
                <property name="use_outer_join">true</property>
                <property name="command_timeout">60</property>
                <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
                <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
                <mapping assembly="NHibernateSample.Domain(实体类程序集的名称)"/>
            </session-factory>
        </hibernate-configuration>
    </configuration>

     

    今天只是一个简单的内容读取。

    NHibernate1.2

    Technorati 标签: NHibernate

     

    代码可以从http://nhibernatedemo.codeplex.com/下载。

     

    常见错误:

    1、未能加载程序集“Domain.Server”的配置

      可能是</hibernate-configuration>中的<mapping assembly="Domain.Server"/>的assembly属性,后面的值要是你的实体类的程序集名称,也就是那个dll的名称
    2    hibernate.cfg.xml,An exception occurred during configuration of persistence layer

    XML文件的默认“复制到输出目录”为“不复制”,这里需要修改为“始终复制”。否则出现“failed: NHibernate.Cfg.HibernateConfigException : An exception occurred during configuration of persistence layer. ----> System.IO.FileNotFoundException : 未能找到文件“NHibernateSample\NHibernateSample.Data.Test\bin\Debug\hibernate.cfg.xml””异常。

    3   No persister for

    XML文件的默认生成操作为“内容”,这里需要修改为“嵌入的资源”生成,因为NHibernate是通过查找程序集中的资源文件映射实体,使用.NET Reflector查看程序集:
    查看程序集
    否则出现“ failed: NHibernate.MappingException : No persister for: NHibernateSample.Domain.Entities.Customer”异常。  

    4   http://www.cnblogs.com/lyj/archive/2009/12/23/1310913.html?page=2

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    redis_ 5 集群
    redis_4 主从模式
    redis_3 持久化
    redis_2 数据类型
    linux_ubuntu 连接xftp
    redis_1 安装和简单使用
    Activiti 各个节点涉及的表
    oracle 数据库imp操作导入dmp文件时表空间问题
    ORA-27101: shared memory realm does not exist 错误的处理
    oralce清理user 和tablespace.
  • 原文地址:https://www.cnblogs.com/virusswb/p/1642402.html
Copyright © 2011-2022 走看看