我在设置NHibernate时遇到了一些麻烦,我不太确定问题到底是什么。我试图将一个域对象保存到数据库(Oracle 10g XE)。但是,在尝试创建ISessionFactory时出现TypeInitializationException。这里是我的hibernate.cfg.xml是什么样子:NHibernate无法创建SessionFactory
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="MyProject.DataAccess">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">
User ID=myid;Password=mypassword;Data Source=localhost
</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.OracleDialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping resource="MyProject/Domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我创建了我会用坚持域对象与数据库的DAO。 DAO使用一个创建SessionFactory的HibernateUtil类。这两个类都与Hibernate配置一起位于DataAccess命名空间中。这是发生异常的地方。这里是该类:
public class HibernateUtil
{
private static ISessionFactory SessionFactory = BuildSessionFactory();
private static ISessionFactory BuildSessionFactory()
{
try
{
// This seems to be where the problem occurs
return new Configuration().Configure().BuildSessionFactory();
}
catch (TypeInitializationException ex)
{
Console.WriteLine("Initial SessionFactory creation failed." + ex);
throw new Exception("Unable to create SessionFactory.");
}
}
public static ISessionFactory GetSessionFactory()
{
return SessionFactory;
}
}
DataAccess命名空间引用NHibernate dll。这与我在Hibernate中使用的Java实际上是一样的设置,所以我不完全确定我在这里做错了什么。有任何想法吗?
编辑
最里面的例外是:
“找不到文件“C:\用户\泰勒\文档\ Visual Studio 2010的\项目\ MyProject的\ MyProject的\ ConsoleApplication \ BIN \调试\ hibernate.cfg.xml中”。”
ConsoleApplication包含创建User对象的入口点,并试图用我的DAO持久化它。为什么在那里寻找配置文件?实际的持久化发生在DataAccess中的DAO中。另外,当我将配置文件添加到ConsoleApplication时,它仍然没有找到它。
后异常的内容的文件夹这个问题将得到解决,请。 – Vadim 2011-01-09 01:00:09
@Yads:我更新了我的帖子。 – 2011-01-09 07:26:21