NHibernate OleDB:使用session.get(id)时程序绑定无效

本文关键字:id 程序 无效 绑定 OleDB 使用 session get NHibernate | 更新日期: 2023-09-27 18:03:28

我是第一次使用NHibernate。我已经将其设置为使用OleDB连接到SQLBase数据库。我已经设置了一些课程,现在尝试了第一个简短的测试,看看它是否有效。

这是我的测试:

[TestClass]
public class Hibernate
{
    private ISessionFactory _sessionFactory;
    private Configuration _config;
    [TestInitialize]
    public void InitTest()
    {
        _config = new Configuration();
        _config.Configure();
        _config.AddAssembly(typeof(Coil).Assembly);
        _sessionFactory = _config.BuildSessionFactory();
    }

    [TestMethod]
    public void TestMethod1()
    {
        using (var session = _sessionFactory.OpenSession())
        {
            Int32 coilid = 12189;
            Coil coil = session.Get<Coil>(coilid);
            Assert.AreEqual(coil.CoilNumber, "6FEB13");
        }
    }
}

这里是映射的相关部分:

<class name="Coil" table="COIL">
<id name="ID" column="COILID" type="integer">
  <generator class="hilo">
    <param name="table">DEFTAB</param>
    <param name="column">WERT1</param>
    <param name="max_lo">10</param>
    <param name="where">TBCODE='LFDNR' AND CODE='WGID'</param>
  </generator>
</id>
<property name="CoilNumber" column="COILNR" type="string" />
</class>

当我尝试运行我的简单测试时,我得到一个GenericADOException:无法加载一个实体。[sql: select…]coil0_.COILID = ?-> system . data . oledb . oledbexcexception: Invalid program bind variable.

当我复制SELECT…coil0_的地方。COILID=12189对SQLbase客户端查询成功,没有任何错误。问题是,不知何故,NHibernate不把我的id变量到查询,而只是放一个?在那里。

我只是在测试中使用错误的语法,还是我有一些配置问题?

最后我的hibernate配置文件,如果它有帮助:

  <session-factory>
<property  name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.GenericDialect</property>
<property name="connection.driver_class">NHibernate.Driver.OleDbDriver</property>
<property name="connection.connection_string">Provider=SQLBASEOLEDB.1;Password=XXX;User ID=XXX;Data Source=XXX</property>
<property name="show_sql">true</property>

再次,SQL输出很好,但我不明白为什么NHibernate不把我用于函数的ID放入SQL查询。

编辑:COILID在数据库表中是一个普通的INTEGER,在Coil类中是一个int。

Edit2:这是堆栈跟踪

System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader()
NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)
NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session)
NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId)
NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session)
NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session)
NHibernate.Event.Default.DefaultLoadEventListener.LoadFromDatasource(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
NHibernate.Impl.SessionImpl.Get[T](Object id)
Tests.Hibernate.TestMethod1() in "D:'Hibernate.cs": Zeile 36

Coil表的创建表语法如下:

CREATE TABLE COIL (
  COILID INTEGER NOT NULL, 
  COILNR VARCHAR(50), 
);

Edit5:查询所有条目的工作,但一旦我想从表中获得一个特定的条目,我得到?再次出错:(

var allCoils = session.CreateCriteria<Coil>().List<Coil>();
Coil datCoil = allCoils.First<Coil>(coil => coil.CoilNumber == "6FEB13");
Assert.AreEqual(datCoil.CoilNumber, "6FEB13");

这个测试至少是成功的

Edit6:问题似乎是nhibernate准备这样的sql:

DEBUG NHibernate.SQL - SELECT coil0_.COILID as COILID1_0_, coil0_.COILNR as COILNR1_0_ FROM COIL coil0_ WHERE coil0_.COILID=?;p0 = 12189

但是SQLbase需要像

这样准备好的字符串
WHERE coil0_.COILID = :0
'
12189
/

我必须做些什么来改变nhibernate将绑定变量转换为SQL的方式?

NHibernate OleDB:使用session.get<T>(id)时程序绑定无效

我能够通过为SQLBase编写自己的驱动程序和方言来解决这个问题(分别来自OleDbDriver和GenericDialect)。如果有人需要使用SQLBase与NHibernate,我可以发布代码。