Почему NHibernate Mapping-by-code игнорирует имя моей таблицы в Oracle:

Я использую базу данных Oracle с NHibernate 3.3.2.4000.

У меня есть модульный тест, настроенный для проверки возможности выбора коллекции сущностей из таблицы. Вот как это выглядит:

[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();
    }
}

Строка, которая извлекает мою коллекцию OAuthMemberships, выдает это исключение:

не может выполнить запрос

[выберите oauthmembe0_.id как id13_ из bckgrd_booklet_app.OAuthMembership oauthmembe0_]

[SQL: выберите oauthmembe0_.id как id13_ из bckgrd_booklet_app.OAuthMembership oauthmembe0_]

Мой класс OAuthMembership и сопоставление приведены ниже. Как видите, я определяю имя таблицы как OAUTH_MEMBERSHIP, но сгенерированный SQL вместо этого включает имя класса в верблюжьем регистре. У меня нет определенных соглашений об именах таблиц. Почему NHibernate игнорирует мои имена таблиц в Oracle?

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"));
    }
}

Вот мой 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();
    }
}

Мой 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;
    }
}

Соответствующие части моего определения UnitOfWork:

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>();
    }

    //...
}

person PancakeParfait    schedule 03.12.2012    source источник


Ответы (1)


Я обнаружил свою ошибку после добавления Fluent NHibernate в свой проект и сделал там ту же ошибку.

У моего OAuthMembershipMap нет конструктора. Вместо этого я по ошибке добавил метод void с именем OAuthMembership, поэтому сопоставление таблиц и сопоставление идентификаторов и свойств завершились неудачно. Смотрите исправленный код:

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"));
    }
}
person PancakeParfait    schedule 04.12.2012