先把我的代码贴一段:nhibernate版本为3.0的版本
helper类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Engine;
namespace Nhibernate
{
public class Hibernatehelper
{
ISessionFactory _isessionfactory = null;
public static Hibernatehelper hibernatehelper = null;
public ISession _session = null;
public static Hibernatehelper GetIntance()
{
if (hibernatehelper == null)
{
hibernatehelper = new Hibernatehelper();
}
return hibernatehelper;
}
private Hibernatehelper()
{
if (_isessionfactory == null)
{
_isessionfactory = GetSessionFactory();
}
if (_session == null)
{
_session = GetSession();
}
}
private ISessionFactory GetSessionFactory()
{
return new Configuration().Configure().BuildSessionFactory();
}
private ISession GetSession()
{
return _isessionfactory.OpenSession();
}
public entity GetEntityById(string id)
{
return _session.Get<entity>(id);
}
}
}
实体类
------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Nhibernate
{
public class entity
{
/// <summary>
/// UserGuid
/// </summary>
public virtual string UserGuid
{
get;
set;
}
/// <summary>
/// UserAccount
/// </summary>
public virtual string UserAccount
{
get;
set;
}
/// <summary>
/// UserName
/// </summary>
public virtual string UserName
{
get;
set;
}
/// <summary>
/// UserEName
/// </summary>
public virtual string UserEName
{
get;
set;
}
}
}
实体映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Nhibernate" namespace="Nhibernate">
<class name="Nhibernate.entity,Nhibernate" table="User">
<id name="UserGuid" column="UserGuid" type="string" unsaved-value="0">
</id>
<property name="UserAccount" column="UserAccount" type="string" />
<property name="UserName" column="UserName" type="string" />
<property name="UserEName" column="UserEName" type="string" />
</class>
</hibernate-mapping>
主配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.OracleClient.dll provider for Oracle from MS -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory >
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=.;Initial Catalog=usajt;Integrated Security=True;Pooling=False
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!--
<property name="query.factory_class">
NHibernate.Hql.Classic.ClassicQueryTranslatorFactory, NHibernate
</property>
-->
<mapping assembly="Nhibernate" resource="Entity.hbm.xml" />
</session-factory>
</hibernate-configuration>
调用程序:
Hibernatehelper.GetIntance().GetEntityById("18eb9884-e8d6-4929-a671-a02f94634732")时发生异常提示:Resource not found: Entity.hbm.xml
且有异常如下:
已引发:“未能加载文件或程序集“NHibernate.XmlSerializers, Version=3.2.0.3001, Culture=neutral, PublicKeyToken=aa95f207798dfdb4”或它的某一个依赖项。系统找不到指定的文件。”(System.IO.FileNotFoundException) 异常消息 = "未能加载文件或程序集“NHibernate.XmlSerializers, Version=3.2.0.3001, Culture=neutral, PublicKeyToken=aa95f207798dfdb4”或它的某一个依赖项。系统找不到指定的文件。", 异常类型 = "System.IO.FileNotFoundException"
如果在主配置文件中设置为程序集的名字则会出现以下异常提示:
Unable to locate persister for the entity named 'Nhibernate.entity'.
The persister define the persistence strategy for an entity.
Possible causes:
- The mapping for 'Nhibernate.entity' was not added to the NHibernate configuration.
但是我的持久的xml是配置入主配置文件的。同时所有的xml文件均为嵌入类型的,且为始终复制
检查这里:
<mapping assembly="Nhibernate" resource="Entity.hbm.xml" />
应该是包含这个映射文件的程序集的名字,而不是这个映射配置文件
注意 Entity.hbm.xml要作为资源嵌入,用官方的 例子对照一下可很快找到问题
哥们 问题解决了吗? 求解啊
在id内加入<generator class="GuidNative"/>
<id name="UserGuid" column="UserGuid" type="String" unsaved-value="0">
</id>
改为:
<id name="UserGuid" column="UserGuid" type="Guid" unsaved-value="0">
<generator class="GuidNative"/>
</id>
<generator class="native"/>是用来创建主键和标识自增的,