I'm having a bit of trouble setting up NHibernate, and I'm not too sure what the problem is exactly. I'm attempting to save a domain object to the database (Oracle 10g XE). However, I'm getting a TypeInitializationException while trying to create the ISessionFactory. Here is what my hibernate.cfg.xml looks like:
<?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>
I created a DAO which I will use to persist domain objects to the database. The DAO uses a HibernateUtil class that creates the SessionFactory. Both classes are in the DataAccess namespace along with the Hibernate configuration. This is where the exception is occuring. Here's tha开发者_如何学Pythont class:
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;
}
}
The DataAccess namespace references the NHibernate DLLs. This is virtually the same setup I've used with Hibernate in Java, so I'm not entirely sure what I'm doing wrong here. Any ideas?
Edit
The innermost exception is the following:
"Could not find file 'C:\Users\Tyler\Documents\Visual Studio 2010\Projects\MyProject\MyProject\ConsoleApplication\bin\Debug\hibernate.cfg.xml'."
ConsoleApplication contains the entry point where I've created a User object and am trying to persist it with my DAO. Why is it looking for the configuration file there? The actual persisting takes place in the DAO, which is in DataAccess. Also, when I add the configuration file to ConsoleApplication, it still does not find it.
It is looking for the configuration file in that directory because that is the default location for NHibernate to look for a configuration file. Note that this is a directory, it has nothing to do with namespaces. You need to set the properties on the hibernate.cfg.xml file in your project to copy to output directory.
I think this issue will be resolved if you can copy the cfg.xml file to the folder that contain the NHibernate assembly
精彩评论