NHibernate unable to create SessionFactory

Posted by Tyler on Stack Overflow See other posts from Stack Overflow or by Tyler
Published on 2011-01-09T00:31:24Z Indexed on 2011/01/09 8:54 UTC
Read the original article Hit count: 236

Filed under:
|
|

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 that 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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate