4

我是第一次使用 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: could not load an entity。[SQL: SELECT ... WHERE coil0_.COILID=?] ---> System.Data.OleDb.OleDbException: 无效的程序绑定变量。

当我将 SELECT ... WHERE coil0_.COILID=12189 复制到 SQLbase 客户端时,查询成功且没有任何错误。问题是 NHibernate 不知何故没有将我的 id 变量放入查询中,而是简单地放入一个 ? 那里。

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

最后我的休眠配置文件是否有帮助:

  <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

线圈表的创建表语法如下:

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

4

1 回答 1

0

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

于 2013-03-22T10:26:32.240 回答