2013-03-12 49 views
4

我第一次使用NHibernate。我已經設置它來使用OleDB連接到SQLBase數據庫。我已經設置了一些我的課程,現在嘗試了第一個短期測試,看看它是否有效。NHibernate OleDB:當使用session.get時無效的程序綁定<T>(id)

這是我的測試:

[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 =?] ---> 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的不把我使用的標識函數轉換爲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語法如下:

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中?

+0

你可以發佈堆棧跟蹤的其餘部分嗎?它可能是一種類型不匹配。數據庫中的INTEGER是帶符號還是無符號? – Firo 2013-03-13 09:53:01

+0

週一我回到辦公室時,我會發布整個堆棧跟蹤。 – 2013-03-13 12:31:43

+0

我在測試堆棧跟蹤中編輯。 – 2013-03-18 07:31:44

回答

0

我能夠通過編寫我自己的SQLBase驅動程序和方言(分別來自OleDbDriver和GenericDialect)來解決此問題。如果其他人需要使用NHibernate的SQLBase,我可以發佈代碼。

相關問題