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

时间:2013-03-12 17:13:42

标签: c# sql nhibernate oledb

我第一次使用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 ... WHERE coil0_.COILID =?] ---&gt; System.Data.OleDb.OleDbException:无效的程序绑定变量。

当我将SELECT ... WHERE 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语法如下:

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的方式?

1 个答案:

答案 0 :(得分:0)

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