2012-01-05 31 views
4

我使用oracle作爲db和流利的Nhibernate進行映射。N-Hibernate中的長字符串與Oracle導致錯誤

下面是我的對象類

public class UserFieldEvent 
    { 
     public virtual int Id { get; set; } 
     public virtual UserFieldBase UserField { get; set; } 
     public virtual EventType EventType { get; set; } 
     public virtual string EventScript { get; set; } 
    } 

屬性EventScript的長度可以是從0到4000。 在我提出的列類型EventScript一個CLOB數據庫。

下面是我的映射類:

public UserFieldEventMap() 
     { 
      Table("TBLDS_USERFIELDEVENT"); 
      Id(x => x.Id).GeneratedBy.Sequence("SEQDS_USERFIELDEVENT"); 
      Map(x => x.EventType).CustomType<EventType>(); 
      Map(x => x.EventScript).CustomSqlType("CLOB"); 
      References(x => x.UserField).Column("USERFIELDBASEID"); 
     } 

現在,每當EventScript的長度大於2000我得到的錯誤「ORA-01461:只能用於插入到LONG列綁定一個LONG值」同時將對象保存到數據庫中。任何人都可以提供幫助。

回答

5

這是.NET提供的System.Data.OracleClient.OracleConnection驅動程序的已知問題。修復方法是使用Oracle提供的ODP.net客戶端Oracle.DataAccess.Client.OracleConnection(請參閱:http://nuget.org/packages/odp.net.x86/)或使用以下解決方法(參考:http://thebasilet.blogspot.be/2009/07/nhibernate-oracle-clobs.html)。

public class CustomOracleDriver : OracleClientDriver 
{ 
    protected override void InitializeParameter(System.Data.IDbDataParameter dbParam, string name, SqlType sqlType) 
    { 
     base.InitializeParameter(dbParam, name, sqlType); 


     // System.Data.OracleClient.dll driver generates an ORA-01461 exception because 
     // the driver mistakenly infers the column type of the string being saved, and 
     // tries forcing the server to update a LONG value into a CLOB/NCLOB column type. 
     // The reason for the incorrect behavior is even more obscure and only happens 
     // when all the following conditions are met. 
     // 1.) IDbDataParameter.Value = (string whose length: 4000 > length > 2000) 
     // 2.) IDbDataParameter.DbType = DbType.String 
     // 3.) DB Column is of type NCLOB/CLOB 

     // The above is the default behavior for NHibernate.OracleClientDriver 
     // So we use the built-in StringClobSqlType to tell the driver to use the NClob Oracle type 
     // This will work for both NCLOB/CLOBs without issues. 
     // Mapping file must be updated to use StringClob as the property type 
     // See: http://thebasilet.blogspot.be/2009/07/nhibernate-oracle-clobs.html 
     if ((sqlType is StringClobSqlType)) 
     { 
      ((OracleParameter)dbParam).OracleType = OracleType.NClob; 
     } 
    } 
} 

你需要更新你的SessionFactory使用該驅動程序,以及更新任何CLOB映射使用StringClob自定義類型

Map(x => x.EventType).CustomSqlType("Clob").CustomType("StringClob"); 
相關問題