NHibernate no puede crear SessionFactory

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 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"/>

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 that class:

public class HibernateUtil
    private static ISessionFactory SessionFactory = BuildSessionFactory();

    private static ISessionFactory BuildSessionFactory()
            // 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?


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.

preguntado el 09 de enero de 11 a las 00:01

Post the contents of the exception please. -

@Yads: I have updated my post. -

2 Respuestas

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.

Respondido el 09 de enero de 11 a las 10:01

Sorry, it's late -- namespace wasn't the word I meant. Setting the file to copy to output was what I was looking for, thanks! - Tyler Treat

I think this issue will be resolved if you can copy the cfg.xml file to the folder that contain the NHibernate assembly

Respondido el 09 de enero de 11 a las 11:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.