Domanda

I am using an Oracle Database with NHibernate 3.3.2.4000.

I have a unit test set up to verify that an entity collection can be selected from the table. Here's what it looks like:

[TestFixture]
public class UnitOfWorkIntegrationTests
{
    private IUnitOfWork _unitOfWork;
    private INHibernateSessionFactory _nHibernateSessionFactory;
    private IActiveSessionManager _activeSessionManager;

    [SetUp]
    public void BeforeEachTest()
    {
        _nHibernateSessionFactory = new NHibernateSessionFactory();
        _activeSessionManager = new ActiveSessionManager();
        _unitOfWork = new UnitOfWork(_nHibernateSessionFactory, _activeSessionManager);
    }

    [Test]
    public void ShouldFetchOAuthMemberships()
    {
        var oauths = _unitOfWork.OAuthMemberships.ToArray();
        oauths.ShouldNotBeNull();
    }
}

The line that fetches my OAuthMemberships collection is throwing this exception:

could not execute query

[ select oauthmembe0_.id as id13_ from bckgrd_booklet_app.OAuthMembership oauthmembe0_ ]

[SQL: select oauthmembe0_.id as id13_ from bckgrd_booklet_app.OAuthMembership oauthmembe0_]

My OAuthMembership class and mapping are below. As you can see I am defining the table name as "OAUTH_MEMBERSHIP", but the generated SQL includes the camel-cased class name instead. I have no table name conventions defined. Why does NHibernate ignore my Oracle-cased table names?

public class OAuthMembership
{
    public virtual int Id { get; set; }
    public virtual string Provider { get; set; }
    public virtual string ProviderUserId { get; set; }

    public virtual UserProfile UserProfile { get; set; }
}

public class OAuthMembershipMap : ClassMapping<OAuthMembership>
{
    public void OAuthMembership()
    {
        Table("OAUTH_MEMBERSHIP");

        Id(x => x.Id, m => m.Column("ID"));
        Property(x => x.Provider, m => m.Column("PROVIDER"));
        Property(x => x.ProviderUserId, m => m.Column("PROVIDER_USER_ID"));
        
        ManyToOne(x => x.UserProfile, m => m.Column("USER_PROFILE_ID"));
    }
}

Here's my NHibernateSessionFactory:

public interface INHibernateSessionFactory
{
    ISession Create();
}

public class NHibernateSessionFactory : INHibernateSessionFactory
{
    private static readonly ILog Log = LogManager.GetLogger(typeof(NHibernateSessionFactory).Name);
    private readonly static ISessionFactory SessionFactory;
    public static string ConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["MyConnection"].Return(x => x.ConnectionString,
                "Data Source=myServer;User ID=bckgrd_booklet_app;Password=myPass;");
        }
    }

    static NHibernateSessionFactory()
    {
        try
        {
            var mapper = new ModelMapper();
            mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());

            HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

            var configure = new NHibernate.Cfg.Configuration().Configure();
            configure.AddMapping(domainMapping);
            configure.BuildMappings();
            configure.DataBaseIntegration(x =>
            {
                x.Driver<OracleClientDriver>();
                x.Dialect<Oracle10gDialect>();
                x.ConnectionStringName = ConnectionString;
            })
            .CurrentSessionContext<WebSessionContext>();
            SessionFactory = configure.BuildSessionFactory();

        }
        catch (Exception ex)
        {
            Log.Error("NHibernateSessionFactory did not initialize correctly.", ex);
            throw;
        }
    }

    public ISession Create()
    {
        Log.Debug("Creating new session.");
        return SessionFactory.OpenSession();
    }
}

My ActiveSessionManager:

public interface IActiveSessionManager
{
    void ClearActiveSession();
    NHibernate.ISession GetActiveSession();
    void SetActiveSession(NHibernate.ISession session);
}

public class ActiveSessionManager : IActiveSessionManager
{
    [ThreadStatic]
    private static ISession _current;

    public ISession GetActiveSession()
    {
        return _current;
    }

    public void SetActiveSession(ISession session)
    {
        _current = session;
    }

    public void ClearActiveSession()
    {
        _current = null;
    }
}

Relevant parts of my UnitOfWork definition:

public interface IUnitOfWork
{
    //...
    IQueryable<OAuthMembership> OAuthMemberships { get; }
    IQueryable<T> All<T>();
    //...
}

public class UnitOfWork : IUnitOfWork
{
    private readonly ISession _session;

    //...

    public IQueryable<OAuthMembership> OAuthMemberships
    {
        get { return All<OAuthMembership>(); }
    }

    public UnitOfWork(
        INHibernateSessionFactory sessionFactory,
        IActiveSessionManager activeSessionManager)
    {
        _session = sessionFactory.Create();
        activeSessionManager.SetActiveSession(_session);
    }

    public IQueryable<T> All<T>()
    {
        return _session.Query<T>();
    }

    //...
}
È stato utile?

Soluzione

I found my error after adding Fluent NHibernate to my project and making the same error there.

My OAuthMembershipMap doesn't have a constructor. I had mistakenly added a void method called OAuthMembership instead, so my table mapping and my Id and Property mappings failed. See the corrected code:

public class OAuthMembershipMap : ClassMapping<OAuthMembership>
{
    public OAuthMembershipMap()
    {
        Table("OAUTH_MEMBERSHIP");

        Id(x => x.Id, m => m.Column("ID"));
        Property(x => x.Provider, m => m.Column("PROVIDER"));
        Property(x => x.ProviderUserId, m => m.Column("PROVIDER_USER_ID"));

        ManyToOne(x => x.UserProfile, m => m.Column("USER_PROFILE_ID"));
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top