2011-12-23 74 views
1

試圖讓NHibernate的與Sybase ASA9工作,並具有命名參數拋出一個錯誤的麻煩:NHibernate的與Sybase ASA9與命名參數

Invalid index 0 for this AsaParameterCollection with Count=0. 

我與NHibernate的ODBC驅動程序和目前ASA司機試圖。我可以直接查詢沒有參數,所以我知道連接正在發生。看起來這個參數沒有被正確地傳遞給ASA,但是我一直在弄清楚我是否做錯了什麼,或者這只是NH和ASA的問題。花了一些腦袋去抓這個,所以也許這對未來還會有幫助。該值是正確的,並且在生成的sql的最後一個片段中可見,並且查詢以等號問號結束,該問號似乎是有效的Sybase,只是不清楚它在哪裏崩潰。

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
    <property name="dialect">NHibernate.Dialect.SybaseASA9Dialect</property> 
    <property name="connection.driver_class">NHibernate.Driver.SybaseAsaClientDriver</property> 
    <property name="connection.connection_string">Uid=;Pwd=;Dsn=</property> 
    <property name="hbm2ddl.keywords">none</property><!--could cause problems? update needed when going to sybase driver instead of odbc--> 
    <property name="show_sql">true</property> 
    <!-- mapping files --> 
    <mapping resource="domain.table.hbm.xml" assembly="Label3700" />--> 
    </session-factory> 
</hibernate-configuration> 

映射

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
       assembly="Label3700" 
       namespace="Label3700.Domain"> 

<class name="menu_item" table="micros.mi_def"> 
    <id name="mi_seq"> 
     <column name="mi_seq" sql-type="integer" not-null="true"/> 
     <generator class="identity" /> 
    </id> 
    <property name="obj_num" /> 
    <property name="name_1" /> 
    <property name="fam_grp_seq" /> 
    </class> 
</hibernate-mapping> 

public class menu_item 
{ 
    public virtual int mi_seq { get; set; } 
    public virtual int obj_num { get; set; } 
    public virtual string name_1 { get; set; } 
    public virtual int fam_grp_seq { get; set; } 
} 

查詢

IList<menu_item> mi = session.CreateQuery("FROM menu_item m where m.fam_grp_seq = :famGrpSeq") 
.SetParameter("famGrpSeq", familyGroupSeq) 
.List<menu_item>(); 

錯誤

could not execute query 
[ select menu_item0_.mi_seq as mi1_1_, menu_item0_.obj_num as obj2_1_,menu_item0_.name_1 as name3_1_, menu_item0_.fam_grp_seq as fam4_1_ from micros.mi_def menu_item0_ where menu_item0_.fam_grp_seq=? ] 
Name:famGrpSeq - Value:44 
[SQL: select menu_item0_.mi_seq as mi1_1_, menu_item0_.obj_num as obj2_1_,menu_item0_.name_1 as name3_1_, menu_item0_.fam_grp_seq as fam4_1_ from micros.mi_def menu_item0_ where menu_item0_.fam_grp_seq=?] 

回答

0

您需要創建自定義連接驅動程序,因爲默認連接驅動程序使用錯誤的設置。升級到更新版本的NHibernate可能會消除這個問題。

針對Sybase我連接驅動我已經重寫這些屬性:

public class SqlAnywhereConnectionDriver : ReflectionBasedDriver 
{ 
    //other code removed… 
    public override bool UseNamedPrefixInSql 
    { 
     get { return true; } 
    } 

    public override bool UseNamedPrefixInParameter 
    { 
     get { return true; } 
    } 

    public override string NamedPrefix 
    { 
     get { return ":"; } 
    } 
}