首页 新闻 会员 周边

我在网上找了一个NHibernateHelper,运行之后~~~求大神指导

0
悬赏园豆:20 [已解决问题] 解决于 2015-11-25 16:25

异常:

未将对象引用设置到对象的实例。
NHIbernateTest01
在 NHIbernateTest01.NHibernateHelper.OpenSession() 位置 C:\Users\Administrator\Desktop\暗夜\Hibernate\NHIbernateTest01\NHIbernateTest01\NHibernateHelper.cs:行号 70
在 NHIbernateTest01.Program.Main(String[] args) 位置 C:\Users\Administrator\Desktop\暗夜\Hibernate\NHIbernateTest01\NHIbernateTest01\Program.cs:行号 26
NHibernate.ISession OpenSession()

NHibernateHelper:(摘自NHibernate的Session管理策略和NHibernateHelper)

public sealed class NHibernateHelper
{
private static ISessionFactory sessionFactory = null;

#region 初始化 生成SessionFactory,并配置上下文策略
public static void Instance(string currentSessionContextClass)
{
sessionFactory = new Configuration().Configure()
.SetProperty("current_session_context_class", currentSessionContextClass)
.BuildSessionFactory();
}
#endregion

#region Session在当前上下文的操作
private static void BindContext()
{
if (!CurrentSessionContext.HasBind(sessionFactory))
{
CurrentSessionContext.Bind(sessionFactory.OpenSession());
}
}

private static void UnBindContext()
{
if (CurrentSessionContext.HasBind(sessionFactory))
{
CurrentSessionContext.Unbind(sessionFactory);
}
}

public static void CloseCurrentSession()
{
if (sessionFactory.GetCurrentSession().IsOpen)
{
sessionFactory.GetCurrentSession().Close();
}
}

public static ISession GetCurrentSession()
{
BindContext();
return sessionFactory.GetCurrentSession();
}
#endregion

#region 关闭SessionFactory(一般在应用程序结束时操作)
public static void CloseSessionFactory()
{
UnBindContext();
if (!sessionFactory.IsClosed)
{
sessionFactory.Close();
}
}
#endregion

#region 打开一个新的Session
public static ISession OpenSession()
{
return sessionFactory.OpenSession();
}
#endregion
}

配置文件:

<configuration>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
</configuration>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.Sql2008Dialect</property>
<property name="show_sql">true</property>
<property name="connection.connection_string">
Data Source=localhost;User Id = sa;Password=123;Persist Security=true;Initial Catalog=TestDB;
</property>
</session-factory>
</hibernate-configuration>
</configuration>

映射文件:

<hibernate-mapping xmlnx="urn:nhibernate-mapping-2.2" assmebly="NHibernateTest01" namespace="NHibernateTest01">
<class name="Person" table="Person">
<id name="Id" column="UserId">
<generator class="guid" />
</id>
<property name="Name" column="Name" />
<property name="Age" column="Age" />
</class>
</hibernate-mapping>

运行程序:

Person p = new Person();
p.Id = Guid.NewGuid();
p.Name = "张三";
p.Age = 29;
try
{

ISession isession = NHibernateHelper.OpenSession();

using (ITransaction trans = isession.BeginTransaction())
{
isession.Save(p);
trans.Commit();
}
}
catch (Exception ex)
{
string str = ex.Message + "\n\r" + ex.Source + "\n\r" + ex.StackTrace + "\n\r" + ex.TargetSite;
File.WriteAllText(@"C:\Users\Administrator\Desktop\暗夜\newLog.txt", str, Encoding.Default);
}

blackday的主页 blackday | 初学一级 | 园豆:118
提问于:2015-11-25 11:13
< >
分享
最佳答案
0

Program.cs:行号 26

这是哪一行?

收获园豆:20
爱编程的大叔 | 高人七级 |园豆:30839 | 2015-11-25 15:40

ISession isession = NHibernateHelper.OpenSession();

blackday | 园豆:118 (初学一级) | 2015-11-25 15:47

在执行的过程中,调用NHibernateHelper.OpenSession()时,报了错

blackday | 园豆:118 (初学一级) | 2015-11-25 15:49

@blackday: 

1、private static ISessionFactory sessionFactory = null;

sessionFactory 是空对象。

2、你没有初始化这个对象

#region 初始化 生成SessionFactory,并配置上下文策略
public static void Instance(string currentSessionContextClass)
{
sessionFactory = new Configuration().Configure()
.SetProperty("current_session_context_class", currentSessionContextClass)
.BuildSessionFactory();
}

3、所以你直接调用NHibernateHelper.OpenSession()的时候,

#region 打开一个新的Session
public static ISession OpenSession()
{
return sessionFactory.OpenSession();
}
#endregion
}

sessionFactory是空对象,这句就会报错的。

爱编程的大叔 | 园豆:30839 (高人七级) | 2015-11-25 16:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册