我一直在尝试使用 Fluent NHibernate 1.2.0.712 针对 Oracle 10g 映射 clob 字段。我正在使用 System.Data 提供程序,因为它默认可用,并试图避免由于以前的客户端问题而添加对 ODP.Net 的引用。
但是,当我尝试插入具有映射的 clob 属性的实体时,出现错误:
ORA-01461: 只能绑定 LONG 值以插入 LONG 列
我尝试通过使用以下约定来解决此问题,并使用 [StringLength(4000)] 装饰适当的属性:
public class StringLengthConvention : AttributePropertyConvention<StringLengthAttribute>
{
protected override void Apply(StringLengthAttribute attribute, IPropertyInstance instance)
{
instance.Length(attribute.MaximumLength);
}
}
这不起作用。
然后我尝试使用“TEXT”、“CLOB”和“clob”值进行以下操作。两者都不起作用:
public class plaparteMappingOverride : IAutoMappingOverride<plaparte>
{
public void Override(AutoMapping<plaparte> mapping)
{
Map(x => x.disposiciones).CustomSqlTypeIs("TEXT");
}
}
除了添加 ODP 作为提供程序之外,还有人对此修复有其他建议吗?
备查:这个帖子 http://thebasilet.blogspot.be/2009/07/nhibernate-oracle-clobs.html完美地描述了导致此错误的原因以及解决方法。
ORA-01461: 只能绑定 LONG 值以插入 LONG 列
这个错误不是很有帮助,仔细观察它很可能会导致
有关 Oracle 补丁等的主题。实际上这是一个
微软 oracle 客户端驱动程序的错误。司机误
推断正在保存的字符串的列类型,并尝试强制
服务器将 LONG 值更新为 CLOB/NCLOB 列类型。这
不正确行为的原因更加模糊且仅
当满足以下所有条件时会发生。
- 当我们设置 IDbDataParameter.Value = (长度为: 4000 > length > 2000 的字符串)
- 当我们设置 IDbDataParameter.DbType = DbType.String
- 当数据库列的类型为 NCLOB/CLOB 时
不幸的是 NHibernate 2.0 的默认行为是完全执行以下操作
上面,使得它更有可能遇到这个丑陋的错误
使用nhibernate和oracle。
博客文章中提供的解决方案:自定义 NHibernate Oracle 驱动程序:
/// <summary>
/// Initializes the parameter.
/// </summary>
/// <param name="dbParam">The db param.
/// <param name="name">The name.
/// <param name="sqlType">Type of the SQL.
protected override void InitializeParameter(System.Data.IDbDataParameter dbParam, string name, global::NHibernate.SqlTypes.SqlType sqlType)
{
base.InitializeParameter(dbParam, name, sqlType);
//System.Data.OracleClient.dll driver generates an exception
//we set the IDbDataParameter.Value = (string whose length: 4000 > length > 2000 )
//when we set the IDbDataParameter.DbType = DbType.String
//when 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 will need to be update to use StringClob as the property type
if ((sqlType is StringClobSqlType))
{
((OracleParameter)dbParam).OracleType = OracleType.NClob;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)