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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xuexiaodong2009/article/details/78674557

问题特别奇怪,按理说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字段完成的名称和所在程序集

展开阅读全文

关于Oracle 10g 字段类型CLOB的写入问题

05-29

这是我的JClob.java文件:rn............rnpublic void write()rn throws GeneralExceptionrnrn String sql = "";rn tryrn conn = daobj.getDBConnection("CommNet");rn conn.setAutoCommit(false);rn sql = "update " + tableName + " set " + fieldName +rn "=empty_clob() where " + primaryKey + "='" + primaryValue + "'";rn rn PreparedStatement pstmt = conn.prepareStatement(sql);rn pstmt.executeUpdate();rnrn sql = "select " + fieldName + " from " + tableName + " where " +rn primaryKey + "='" + primaryValue + "' for update";rn Statement st = conn.createStatement();rn ResultSet rs = st.executeQuery(sql);rnrn java.sql.Clob clob = null;rn if (rs.next()) rn //((oracle.jdbc.OracleResultSet)rs).getClob(fieldName);rn clob = ((oracle.jdbc.OracleResultSet)rs).getClob(fieldName); rn CLOB my_clob = (CLOB)clob;rn if(my_clob!=null)rn //clob = ((org.apache.commons.dbcp.DelegatingResultSet)rs).getClob(fieldName); rn OutputStream writer = my_clob.getAsciiOutputStream(); //好像是这个语句有问题,getAsciiOutputStream()在JB中出现了警告rn byte[] contentStr = this.getContent().getBytes(); rn writer.write(contentStr); rn writer.flush(); rn writer.close(); rn conn.commit();rn rn rn conn.setAutoCommit(true);rn rs.close();rn st.close();rn pstmt.close();rn rn catch(SQLException sqlexception)rn rn throw new GeneralException(sqlexception.getMessage(), 1);rn rn catch(Exception exception)rn rn throw new GeneralException(exception.getMessage(), 1);rn rn finallyrn rn rn rnrn.........rnrn下面的我的JSP程序.jsprn...rnif(actionflag.equals("edit"))rn String GoalsID = objWebtools.codeToLocal("CommNet",request.getParameter("GoalsID"));rn //result = objGoals.UpdateGoals(as,GoalsID);rn JClob jclob = new JClob("Com_Goals","GoalsID",GoalsID,"CONTENT",as[2]);rn jclob.write();rnrn...rn出现如下问题:rnjavax.servlet.ServletExceptionrn org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)rn org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)rn org.apache.jsp.system.Goals.SaveGoals_jsp._jspService(SaveGoals_jsp.java:170)rn org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:856)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:856)rnrnrnroot cause rnrnCommNet.util.GeneralExceptionrn CommNet.util.JClob.write(JClob.java:171)rn org.apache.jsp.system.Goals.SaveGoals_jsp._jspService(SaveGoals_jsp.java:137)rn org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:856)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:856)rn 论坛

没有更多推荐了,返回首页