nhibernate 新手常见问题


nhibernate1.05 下测试

1
*.hbm.xml
文件在vs2003里要做为Embedded Resources
If VisualStudio.NET or NAnt is used to build the Assembly then make sure that the hbm.xml files are added as Embedded Resources

2 asp.net项目配置要放到web.config中(如果多数库可单为nibernate节点配置xml文件)
 
<configuration>
<configSections>
            
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    
</configSections>

<nhibernate>
.
</nhibernate>
<log4net>
..
</log4net>
<system.web>
.
</system.web>

</configuration>



3
如果类和*.hbm.xml 文件放到了一个Class Lib的项目里。如何引用?
就是说,我新建了一个Solution包含一个webproject和一个Class Lib project
而关于实体类和映射文件放到Class Lib project里来处理以使项目清晰

http://abluedog.cnblogs.com/archive/2006/04/17/377630.html

使用
cfg.AddAssembly(Assembly.GetExecutingAssembly() );
可以自动寻找类与xml配置文件的信息

注意:
如果有下面的配置文件,
Assemmbly的名字一定要是HIHI,就是Class Lib的项目的生成的dll名要为HIHI
<class name="Business.Data.Employees,HIHI" table="Employees">
等同于
<class name="Employees" table="Employees" namespace="Business.Data" assembly="HIHI">

如果项目名和assembly名不同,可选项目->属性->assembly name 进行修改

(无意中犯下了这个错误..)

4

在映射文件中
byte[] 在映射时要写成Byte[],否则不加载,并提示不认识byte[]类型,(在配置文件也可以写成binary类型)


// 5.26.2006
// add by day


1

1.1
将src\NHibernate下
*.xsd
放到
C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml
在vs中编写*.hbm.xml中会有属性提示

1.2
 log4net是通过nunit保存日志的
 所以如果你想偷懒不写unit测试,很难理解到nhibernate的运行过程和输出结果
//by day
//这里讲错了
//打开 log4net,会输出记录,便于分析执行了哪些操作

1.3
在*.hbm.xml映射文件中
可以将使用到的HQL,sql语句单独列出来像 ibatis那样
以使项目清晰

<query name = "">
from Product
</query>

<sql-query name="">
select * from product
</sql-query>

1.4

native sql 语句 使用session.CreateSQLQuery()
select  {su.*} from Suppliers su
select  su.* from Suppliers su 这个会出错..

对原生sql的写法有要求..

2 注意

 如果数据库字段可为空。。
 则不能在类属性的Setter中对value进行判断或操作,不然会提示
 System.NullReferenceException: 未将对象引用设置到对象的实例。
 (MyGeneration的别人共享的Nhibernate模板生成的会有这个问题)



原文地址:https://www.cnblogs.com/day/p/408461.html