<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-72  評(píng)論-63  文章-0  trackbacks-0
    在網(wǎng)上閑逛找到了解決方案:
    ???? 現(xiàn)在3.x中對blob和clob增加了org.hibernate.lob.SerializableBlob和org.hibernate.lob.SerializableClob類的封裝。

    其次如果你將前面的測試程序放到weblogic的容器中通過weblogic的數(shù)據(jù)源得到連接的話,你會(huì)發(fā)現(xiàn)oracle.sql.BLOB blob = (oracle.sql.BLOB)person.getImage();和 oracle.sql.CLOB clob = (oracle.sql.CLOB)person.getArticle();這倆行會(huì)出錯(cuò),原因就是weblogic進(jìn)行了包裝。

    現(xiàn)在將以上兩個(gè)問題的綜合解決方案用以下代碼說明:



    ????????????for (int i = 0; i < 10; i++) {
    ????????????????LargeObject large = new LargeObject();
    ????????????????large.setId(i + "");
    ????????????????large.setName("林意煒");

    ????????????????// 插入一個(gè)小數(shù)據(jù)數(shù)據(jù)
    ????????????????large.setImage(Hibernate.createBlob(new byte[1]));
    ????????????????large.setArticle(Hibernate.createClob(" "));

    ????????????????session.save(large);
    ????????????????session.flush();

    ????????????????// 鎖定該記錄
    ????????????????session.refresh(large, LockMode.UPGRADE);

    ????????????????// 插入圖片數(shù)據(jù)
    ????????????????String fileName = "E:/AAA/" + i + ".jpg";
    ????????????????SerializableBlob sb = (SerializableBlob)large.getImage();
    ????????????????java.sql.Blob wrapBlob = sb.getWrappedBlob();
    ????????????????// 通過非weblogic容器中數(shù)據(jù)源獲得連接的情況
    ????????????????if(wrapBlob instanceof oracle.sql.BLOB){
    ????????????????????oracle.sql.BLOB blob = (oracle.sql.BLOB) wrapBlob;
    ????????????????????OutputStream out = blob.getBinaryOutputStream();
    ????????????????????out.write(getData(fileName));
    ????????????????????out.close();
    ????????????????}
    ????????????????// 使用weblogic的Oracle Thin driver類型連接池,驅(qū)動(dòng)類名:oracle.jdbc.OracleDriver
    ????????????????else if(wrapBlob instanceof weblogic.jdbc.vendor.oracle.OracleThinBlob){
    ????????????????????OracleThinBlob blob = (OracleThinBlob)wrapBlob;
    ????????????????????OutputStream out = blob.getBinaryOutputStream();
    ????????????????????out.write(getData(fileName));
    ????????????????????out.close();
    ????????????????}


    ????????????????// 插入文章數(shù)據(jù)
    ????????????????fileName = "E:/AAA/" + i + ".java";
    ????????????????SerializableClob cb = (SerializableClob)large.getArticle();
    ????????????????java.sql.Clob wrapClob = cb.getWrappedClob();
    ????????????????// 通過非weblogic容器中數(shù)據(jù)源獲得連接的情況
    ????????????????if(wrapClob instanceof oracle.sql.CLOB){
    ????????????????????oracle.sql.CLOB clob = (oracle.sql.CLOB) wrapClob;
    ????????????????????Writer writer = clob.getCharacterOutputStream();
    ????????????????????String article = new String(getData(fileName));
    ????????????????????writer.write(article);
    ????????????????????writer.close();
    ????????????????}
    ????????????????// 使用weblogic的Oracle Thin driver類型連接池,驅(qū)動(dòng)類名:oracle.jdbc.OracleDriver
    ????????????????else if(wrapClob instanceof weblogic.jdbc.vendor.oracle.OracleThinClob){
    ????????????????????OracleThinClob clob = (OracleThinClob)wrapClob;
    ????????????????????Writer writer = clob.getCharacterOutputStream();
    ????????????????????String article = new String(getData(fileName));
    ????????????????????writer.write(article);
    ????????????????????writer.close();
    ????????????????}
    ????????????}


    ***************************************************
    采用得是ORACLE9i數(shù)據(jù)庫,Jboss或Weblogic。
    JDBC采用ORACLE9i自帶的Class12.jar
    -------------
    數(shù)據(jù)庫結(jié)構(gòu):
    java代碼:?


    CREATE TABLE SNCPARAMETERS
    (
    ? ID? ? ?NUMBER(19)? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NOT NULL,
    ? SNCID? NUMBER(19),
    ? NAME? ?VARCHAR2(255),
    ? VALUE? CLOB
    )


    --------------
    BO采用xdoclet建立的:
    java代碼:?


    publicclass SNCParameters extends BaseObject
    {

    ? ? /**
    ? ? ?* Returns the id.
    ? ? ?*
    ? ? ?* @return? ? ? long
    ? ? ?* @hibernate.id
    ? ? ?*? ? ? ? ? column = "id"
    ? ? ?*? ? ? ? ? type = "long"
    ? ? ?*? ? ? ? ? generator-class = "native"
    ? ? ?*? ? ? ? ? unsaved-value = "null"
    ? ? ?*/

    ? ? publicLong getId()
    ? ? {
    ? ? ? ? return id;
    ? ? }

    ? ? /**
    ? ? ?*? ? Sets the Id attribute of the SNCParameters object
    ? ? ?*
    ? ? ?* @param? ? id? The new Id value
    ? ? ?*/

    ? ? publicvoid setId(Long id)
    ? ? {
    ? ? ? ? this.id = id;
    ? ? }


    ? ? /**
    ? ? ?* Returns the name.
    ? ? ?*
    ? ? ?* @return? ? ? String
    ? ? ?*
    ? ? ?* @hibernate.property
    ? ? ?*? ? ? ? ? column = "name"
    ? ? ?*? ? ? ? ? type = "string"
    ? ? ?*? ? ? ? ? not-null = "true"
    ? ? ?*? ? ? ? ? unique = "false"
    ? ? ?*/


    ? ? publicString getName()
    ? ? {
    ? ? ? ? return name;
    ? ? }

    ? ? /**
    ? ? ?*? ? Sets the Name attribute of the SNCParameters object
    ? ? ?*
    ? ? ?* @param? ? name? The new Name value
    ? ? ?*/

    ? ? publicvoid setName(String name)
    ? ? {
    ? ? ? ? this.name = name;
    ? ? }

    ? ? /**
    ? ? ?* Returns the sncId.
    ? ? ?*
    ? ? ?* @return? ? ? Long
    ? ? ?*
    ? ? ?* @hibernate.property
    ? ? ?*? ? ? ? ? column = "sncId"
    ? ? ?*? ? ? ? ? type = "long"
    ? ? ?*? ? ? ? ? not-null = "true"
    ? ? ?*? ? ? ? ? unique = "false"
    ? ? ?*/


    ? ? publicLong getSncId()
    ? ? {
    ? ? ? ? return sncId;
    ? ? }

    ? ? /**
    ? ? ?*? ? Sets the SncId attribute of the SNCParameters object
    ? ? ?*
    ? ? ?* @param? ? sncId? The new SncId value
    ? ? ?*/

    ? ? publicvoid setSncId(Long sncId)
    ? ? {
    ? ? ? ? this.sncId = sncId;
    ? ? }

    ? ? /**
    ? ? ?* Returns the values.
    ? ? ?*
    ? ? ?* @return? ? ? Clob
    ? ? ?*
    ? ? ?* @hibernate.property
    ? ? ?*? ? ? ? ? column = "value"
    ? ? ?*? ? ? ? ? type = "clob"
    ? ? ?*? ? ? ? ? not-null = "true"
    ? ? ?*? ? ? ? ? unique = "false"
    ? ? ?*/


    ? ? publicClob getValue()
    ? ? {
    ? ? ? ? return value;
    ? ? }

    ? ? /**
    ? ? ?*? ? Sets the Values attribute of the SNCParameters object
    ? ? ?*
    ? ? ?* @param? ? values? The new Values value
    ? ? ?*/

    ? ? publicvoid setValue(Clob value)
    ? ? {
    ? ? ? ? this.value = value;
    ? ? }
    ? ? privateLong id;
    ? ? privateLong sncId;
    ? ? privateString name;
    ? ? privateClob value;
    ? ? privateString valueString;
    ? ? publicString getValueString()
    ? ? {
    ? ? ? ? return valueString;
    ? ? }
    ? ? ? ? publicvoid setValueString(String? valueString)
    ? ? {
    ? ? ? ? this.valueString = valueString;
    ? ? }
    }



    注:valueString并不映射到數(shù)據(jù)庫的CLOB字段,只是方便需要使用這個(gè)BO的人用GET、SET 處理這個(gè)巨長的CLOB字段
    ------------
    xdocLet生成的XML文件:
    java代碼:?


    <?xml version="1.0"?>

    <!DOCTYPE hibernate-mapping PUBLIC
    ? ? "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    ? ? "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

    <hibernate-mapping>
    ? ? <class
    ? ? ? ? name="com.idncn.mc.bo.SNCParameters"
    ? ? ? ? table="SNCParameters"
    ? ? ? ? dynamic-update="false"
    ? ? ? ? dynamic-insert="false"
    ? ? >

    ? ? ? ? <id
    ? ? ? ? ? ? name="id"
    ? ? ? ? ? ? column="id"
    ? ? ? ? ? ? type="long"
    ? ? ? ? ? ? unsaved-value="null"
    ? ? ? ? >
    ? ? ? ? ? ? <generator class="native">
    ? ? ? ? ? ? </generator>
    ? ? ? ? </id>

    ? ? ? ? <property
    ? ? ? ? ? ? name="name"
    ? ? ? ? ? ? type="string"
    ? ? ? ? ? ? update="true"
    ? ? ? ? ? ? insert="true"
    ? ? ? ? ? ? column="name"
    ? ? ? ? ? ? not-null="true"
    ? ? ? ? ? ? unique="false"
    ? ? ? ? />

    ? ? ? ? <property
    ? ? ? ? ? ? name="sncId"
    ? ? ? ? ? ? type="long"
    ? ? ? ? ? ? update="true"
    ? ? ? ? ? ? insert="true"
    ? ? ? ? ? ? column="sncId"
    ? ? ? ? ? ? not-null="true"
    ? ? ? ? ? ? unique="false"
    ? ? ? ? />

    ? ? ? ? <property
    ? ? ? ? ? ? name="value"
    ? ? ? ? ? ? type="clob"
    ? ? ? ? ? ? update="true"
    ? ? ? ? ? ? insert="true"
    ? ? ? ? ? ? column="value"
    ? ? ? ? ? ? not-null="true"
    ? ? ? ? ? ? unique="false"
    ? ? ? ? />
    ? ? </class>

    </hibernate-mapping>


    --------------------
    insert的代碼:
    java代碼:?


    ? ? publicList batchAddSncParameters(List sncParametersList, Long sncId)throws DbAccessException
    ? ? {
    ? ? ? ? logger.enterMethod();
    ? ? ? ? List ret = newArrayList();
    ? ? ? ? try
    ? ? ? ? {
    ? ? ? ? ? ? sess = getSession();
    ? ? ? ? ? ? if(sncParametersList != null && sncParametersList.size() > 0)
    ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? for(int i = 0; i < sncParametersList.size(); i++)
    ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? SNCParameters cp = (SNCParameters) sncParametersList.get(i);
    ? ? ? ? ? ? ? ? ? ? long newId = -1;
    ? ? ? ? ? ? ? ? ? ? if(cp != null)
    ? ? ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? ? ? SNCParameters cpNew = new SNCParameters();
    ? ? ? ? ? ? ? ? ? ? ? ? cpNew.setSncId(sncId);
    ? ? ? ? ? ? ? ? ? ? ? ? cpNew.setName(cp.getName());
    ? ? ? ? ? ? ? ? ? ? ? ? cpNew.setValue(Hibernate.createClob(" "));
    ? ? ? ? ? ? ? ? ? ? ? ? newId = ((Long) sess.save(cpNew)).longValue();
    ? ? ? ? ? ? ? ? ? ? ? ? sess.flush();

    ? ? ? ? ? ? ? ? ? ? ? ? sess.refresh(cpNew, LockMode.UPGRADE);
    ? ? ? ? ? ? ? ? ? ? ? ? String content = cp.getValueString();

    ? ? ? ? ? ? ? ? ? ? ? ? String appserver = System.getProperty("appserver", "jboss");
    ? ? ? ? ? ? ? ? ? ? ? ? if(!appserver.equalsIgnoreCase("jboss"))
    ? ? ? ? ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? //weblogic
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? OracleThinClob clob = (OracleThinClob) cpNew.getValue();
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? java.io.Writer pw = clob.getCharacterOutputStream();
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? pw.write(content);
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? pw.flush();
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? pw.close();
    ? ? ? ? ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? ? ? ? ? ? ? else
    ? ? ? ? ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? //jboss
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? oracle.sql.CLOB clob = (oracle.sql.CLOB) cpNew.getValue();
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? java.io.Writer pw = clob.getCharacterOutputStream();
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? pw.write(content);
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? pw.flush();
    ? ? ? ? ? ? ? ? ? ? ? ? ? ? pw.close();
    ? ? ? ? ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? ? ? ? ? ? ? ret.add(newLong(newId));
    ? ? ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? }
    ? ? ? ? }
    ? ? ? ? catch(Exception e)
    ? ? ? ? {
    ? ? ? ? ? ? logger.error(e);
    ? ? ? ? ? ? ErrorReason errorReason = new ErrorReason(ErrorReason.INSERT_OBJECT_FAILED_REASON);
    ? ? ? ? ? ? throw new DbAccessException(DbAccessException.DBA_OPERATE_EXCEPTION, errorReason);
    ? ? ? ? }
    ? ? ? ? finally
    ? ? ? ? {
    ? ? ? ? ? ? closeSession(sess);
    ? ? ? ? ? ? logger.exitMethod();
    ? ? ? ? }
    ? ? ? ? return ret;
    ? ? }


    -----------------
    注:Weblogic必須使用weblogic.jdbc.vendor.oracle.OracleThinClob
    ---------------------
    讀取CLOB字段:
    java代碼:?


    ? ? publicList selectSncParametersBySncId(long sncId)throws DbAccessException
    ? ? {
    ? ? ? ? logger.enterMethod();
    ? ? ? ? List ret = newArrayList();
    ? ? ? ? try
    ? ? ? ? {
    ? ? ? ? ? ? sess = getSession();
    ? ? ? ? ? ? String query = "select cp from cp in class com.idncn.mc.bo.SNCParameters where cp.sncId = ?";
    ? ? ? ? ? ? logger.debug("SQL=" + query);
    ? ? ? ? ? ? List iter = sess.find(query, newLong(sncId), Hibernate.LONG);
    ? ? ? ? ? ? for(int i = 0; i < iter.size(); i++)
    ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? SNCParameters newCp = new SNCParameters();
    ? ? ? ? ? ? ? ? SNCParameters cp = (SNCParameters)(iter.get(i));
    ? ? ? ? ? ? ? ? logger.debug("after fetch:" + cp);
    ? ? ? ? ? ? ? ? newCp.setId(cp.getId());
    ? ? ? ? ? ? ? ? newCp.setSncId(cp.getSncId());
    ? ? ? ? ? ? ? ? newCp.setName(cp.getName());
    ? ? ? ? ? ? ? ? java.sql.Clob clob = cp.getValue();
    ? ? ? ? ? ? ? ? if(clob != null)
    ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? logger.debug("b===" + clob.length());
    ? ? ? ? ? ? ? ? ? ? String b = clob.getSubString(1, (int) clob.length());
    ? ? ? ? ? ? ? ? ? ? //logger.debug("b==="+b);
    ? ? ? ? ? ? ? ? ? ? newCp.setValueString(b);
    ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? ? ? ret.add(newCp);
    ? ? ? ? ? ? }
    ? ? ? ? }
    ? ? ? ? catch(Exception e)
    ? ? ? ? {
    ? ? ? ? ? ? logger.error(e);
    ? ? ? ? ? ? ErrorReason errorReason = new ErrorReason(ErrorReason.SELECT_FAILED_REASON);
    ? ? ? ? ? ? throw new DbAccessException(DbAccessException.DBA_OPERATE_EXCEPTION, errorReason);
    ? ? ? ? }
    ? ? ? ? finally
    ? ? ? ? {
    ? ? ? ? ? ? closeSession(sess);
    ? ? ? ? ? ? logger.exitMethod();
    ? ? ? ? }
    ? ? ? ? return ret;
    ? ? }


    ---------------
    更新這個(gè)字段的代碼:
    java代碼:?


    ? ? publicvoid updateSncParameters(SNCParameters newParam)throws DbAccessException
    ? ? {
    ? ? ? ? logger.enterMethod();
    ? ? ? ? try
    ? ? ? ? {
    ? ? ? ? ? ? sess = getSession();

    ? ? ? ? ? ? Long id = newParam.getId();
    ? ? ? ? ? ? SNCParameters pp = (SNCParameters) sess.load(SNCParameters.class, id, net.sf.hibernate.LockMode.UPGRADE);

    ? ? ? ? ? ? pp.setSncId(newParam.getSncId());
    ? ? ? ? ? ? pp.setName(newParam.getName());
    ? ? ? ? ? ? pp.setId(newParam.getId());

    ? ? ? ? ? ? String newValue = newParam.getValueString();
    ? ? ? ? ? ? logger.debug("Update Length =" + newValue.length());

    ? ? ? ? ? ? String appserver = System.getProperty("appserver", "jboss");
    ? ? ? ? ? ? logger.debug("appserver: " + appserver);
    ? ? ? ? ? ? if(!appserver.equalsIgnoreCase("jboss"))
    ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? //weblogic
    ? ? ? ? ? ? ? ? OracleThinClob clob = (OracleThinClob) pp.getValue();
    ? ? ? ? ? ? ? ? if(pp != null)
    ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? java.io.Writer pw = clob.getCharacterOutputStream();
    ? ? ? ? ? ? ? ? ? ? pw.write(newValue);
    ? ? ? ? ? ? ? ? ? ? pw.flush();
    ? ? ? ? ? ? ? ? ? ? pw.close();
    ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? }
    ? ? ? ? ? ? else
    ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? //jboss
    ? ? ? ? ? ? ? ? oracle.sql.CLOB clob = (oracle.sql.CLOB) pp.getValue();
    ? ? ? ? ? ? ? ? if(pp != null)
    ? ? ? ? ? ? ? ? {
    ? ? ? ? ? ? ? ? ? ? java.io.Writer pw = clob.getCharacterOutputStream();
    ? ? ? ? ? ? ? ? ? ? pw.write(newValue);
    ? ? ? ? ? ? ? ? ? ? pw.flush();
    ? ? ? ? ? ? ? ? ? ? pw.close();
    ? ? ? ? ? ? ? ? }
    ? ? ? ? ? ? }
    ? ? ? ? }
    ? ? ? ? catch(Exception e)
    ? ? ? ? {
    ? ? ? ? ? ? logger.error(e);
    ? ? ? ? ? ? ErrorReason errorReason = new ErrorReason(ErrorReason.UPDATE_OBJECT_FAILED_REASON);
    ? ? ? ? ? ? throw new DbAccessException(DbAccessException.DBA_OPERATE_EXCEPTION, errorReason);
    ? ? ? ? }
    ? ? ? ? finally
    ? ? ? ? {
    ? ? ? ? ? ? closeSession(sess);
    ? ? ? ? ? ? logger.exitMethod();
    ? ? ? ? }
    ? ? }


    posted on 2006-07-11 11:07 船長 閱讀(1802) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE
    主站蜘蛛池模板: 亚洲乱妇老熟女爽到高潮的片| 九九精品免费视频| 亚洲AV无码一区二区三区牲色 | 久久亚洲日韩精品一区二区三区| 欧洲美熟女乱又伦免费视频| 最近免费中文字幕mv电影| 国产成人自产拍免费视频| 亚洲AV无码一区二区三区久久精品| 亚洲黄色在线电影| 亚洲国产成人高清在线观看| 亚洲AV无码乱码在线观看| 免费的一级片网站| 日韩视频在线精品视频免费观看| 久久久久久国产精品免费免费男同| 在线播放国产不卡免费视频| 午夜亚洲国产理论片二级港台二级| 自拍日韩亚洲一区在线| 亚洲欧洲日韩综合| 91亚洲国产成人久久精品网站| 亚洲gv猛男gv无码男同短文| 亚洲夜夜欢A∨一区二区三区| 亚洲人成无码网WWW| vvvv99日韩精品亚洲| 国产免费av片在线无码免费看| 毛片大全免费观看| 国产成人免费高清激情视频| 日韩视频在线精品视频免费观看 | 亚洲成色www久久网站夜月| 亚洲午夜无码AV毛片久久| 亚洲AV网站在线观看| 国产免费午夜a无码v视频| 国产无遮挡吃胸膜奶免费看视频 | 亚洲乱码一区av春药高潮| 亚洲欧洲日本天天堂在线观看| 亚洲自偷精品视频自拍| 亚洲精品91在线| 久久亚洲精品国产精品婷婷| 亚洲AV日韩综合一区尤物| 国产精品亚洲lv粉色| 色婷婷综合缴情综免费观看| 永久免费无码日韩视频|