NHibernate中Oracle字段类型Clob数据长度过大问题

问题特别奇怪,按理说Clob字段不存在长度过大的问题,但在实际使用中却会提示这个问题。

而且字符特别少时,没有问题,特别长时也没有问题,长度在在1千到五千之前就会有问题。

实际上因为把Clob字段作为varchar所以会出现长度问题。修改如下:

添加类继承NHibernate中的IUserType

 public abstract class PatchForOracleLobField : IUserType
    {
        public PatchForOracleLobField()
        {
        }
        public bool IsMutable
        {
            get { return true; }
        }
        public System.Type ReturnedType
        {
            get { return typeof(String); }
        }
        public SqlType[] SqlTypes
        {
            get
            {
                return new SqlType[] { NHibernateUtil.String.SqlType };
            }
        }
        public object DeepCopy(object value)
        {
            return value;
        }
        public new bool Equals(object x, object y)
        {
            return x == y;
        }
        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }
        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }
        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }
        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            return NHibernate.NHibernateUtil.StringClob.NullSafeGet(rs, names[0]);
        }
        public abstract void NullSafeSet(IDbCommand cmd, object value, int index);
        public object Replace(object original, object target, object owner)
        {
            return original;
        }

    }

重写clob字段

 public class OracleClobField : PatchForOracleLobField
    {
        public override void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (cmd is OracleCommand)
            {
                //CLob、NClob类型的字段,存入中文时参数的OracleDbType必须设置为OracleDbType.Clob
                //否则会变成乱码(Oracle 10g client环境)
                OracleParameter param = cmd.Parameters[index] as OracleParameter;
                if (param != null)
                {
                    param.OracleType = OracleType.Clob;// 关键就这里啦
                    param.IsNullable = true;
                }
            }
            NHibernate.NHibernateUtil.StringClob.NullSafeSet(cmd, value, index);
        }
    }
实际使用
 [ActiveRecord("EAS.T_ProdGroupActivity")]
    public class ProdGroupActivity:  EasActiveRecordCRUDEx<ProdGroupActivity>
    {	
		
		#region property
		
		/// <summary>
		/// Property PGA_ID
		/// </summary>
		[PrimaryKey(PrimaryKeyType.Sequence, "PGA_ID",SequenceName = "EAS.Seq_ProdGroupActivity")]
		public int PGA_ID
		{
            get;
            set;
        }       
		/// <summary>
		/// Property PGA_Content
		/// </summary>
		[Property("PGA_Content", "DHC.EAS.Entity.OracleClobField,Eas.Entity")]
		public string PGA_Content
		{
            get;
            set;
        }		
		#endregion
	}
核心是修改特性[Property("PGA_Content", "DHC.EAS.Entity.OracleClobField,Eas.Entity")]

第一个参数是映射的字段,第二个参数是新定义的clob字段完成的名称和所在程序集

相关文章

相关标签/搜索