刚开始学习NHibernate

本文是继NHibernate Step by Step (一) Hello,NHibernate!   的Oracle版本及做了一些调整。我看了文章后试了一下,花了近半个小时的时间才弄好最基本的配置。这篇就作为一个补充吧。

我的环境的WindowXP VS.net2003 Oracle9i  CodeSmith 3.1.6 

大家可以关注两方面的内容,一个是Oracle 一个是VS.Net2003。这两方面是与原作者的不同。我会把差别的地方都写下来。

至于CodeSmith的配置请看我另一篇Blogs  开始使用CodeSmith3.1-解决了Oracle连接及汉字的问题  

基本的软件环境如下:
1.NHibernate www.nhibernate.org 当前版本是1.0.2
2.Code Smith http://www.codesmithtools.com/  我用的是3.1
3.NHibernate模板 点击这里下载


一、创建表的脚本为:
alter table Person
   drop primary key cascade
/
drop table Person cascade constraints
/
/*==============================================================*/
/* Table: Person                                                */
/*==============================================================*/
create table Person  (
   ID                   number                           not null,
   Name                 VARCHAR2(50)
)
/
alter table Person
   add constraint PK_PERSON primary key (ID)
/

2、打开的时候是把下载下来的nhibernate-1.0.2.0解压后打开nhibernate-1.0.2.0\src这个目录里的NHibernate-1.1.sln这个解决方案。然后再按作者说的添加Model项目等。
3、Person.hbm.xml文件属性里是设置第一个属性:生成操作,默认是内容吧,改成嵌入的资源。(我是用中文版,因为我比较懒)
4、很重要的要注意,在Oracle里生成的代码中,所有的表名和字段名都会变成大写的。可能是因为Oracle的系统表存的是大写的吧。所以要注意一下。
5、修改配置文件:
改成为:(需要修改版本及连接数据库的类型改为Oracle9i)
    <configSections>
       <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <nhibernate>
    <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    <add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" />
    <add key="hibernate.connection.connection_string" value="Data Source=eoffice;user id=test;password=test" />
    <add key="hibernate.connection.isolation" value="ReadCommitted"/>
    <add key="hibernate.dialect" value="NHibernate.Dialect.Oracle9Dialect" />
  </nhibernate>
6、因为我不会用控制台程序,主要是不知道如何修改控制台程序的配置文件,我暂时没有找到,有谁会可以教我一下。所以我前台的程序是用Web程序来做测试的。因为我是用Web程序所以默认就有了using System.Collections;,不需要再引用using System.Collections.Generic;
 
7、因为Oracle没有像SQL Server那个有一个自动增长的字段,所以需要注意一下修改PERSON.hbm.xml文件中的ID的generator 要由原来的native改成assigned
        <id name="Id" type="Int32" unsaved-value="0">
                        <column name="ID" sql-type="NUMBER" not-null="true" unique="true" index="PK_PERSON"/>
                        <generator class="assigned" />
                </id>


       再次提醒,Oracle里生成的大小写一定要注意。我在这方面花了十几分钟才找到原因。累呀

posted @ 2006-08-24 09:53  小草  阅读(1153)  评论(2编辑  收藏  举报
Google+