七郎's JavaBlog

          草木竹石皆可為劒。至人之用人若鏡,不將不迎,應(yīng)而不藏,故能勝物而不傷。
          posts - 60, comments - 14, trackbacks - 0, articles - 0

          hibernate中clob處理

          Posted on 2007-04-16 15:37 七郎歸來(lái) 閱讀(1933) 評(píng)論(0)  編輯  收藏

          用hibernate封裝oracle的clob類型操作起來(lái)很不方便,但是新的oracle10g的jdbc驅(qū)動(dòng),對(duì)此有很多改進(jìn)。

          環(huán)境 :wodows2000、oracle9.2、oracle10gJDBC驅(qū)動(dòng)(必須)、hibernate3.1.2

          oracle官方網(wǎng)站的clob操作方法:http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/clob10g/ClobManipulationIn10g.java.html

          建表腳本:

          /*==============================================================*/
          /* Table: StoreFile                                             */
          /*==============================================================*/
          create table StoreFile  (
             uuid                 VARCHAR2(128)                   not null,
             name                 VARCHAR2(200),
             text                 CLOB,
             constraint PK_STOREFILE primary key (uuid)
          );

          hibernate.cfg.xml:(注意多了個(gè)SetBigStringTryClob設(shè)置,其它沒(méi)有什么特殊的了

          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "
          http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
          <hibernate-configuration>
              <session-factory>
                  <property name="hibernate.cglib.use_reflection_optimizer">true</property>
                  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
                  <property name="hibernate.connection.password">java</property>
                  <property name="hibernate.connection.url">jdbc:oracle:thin:@10.128.4.69:1521:kf</property>
                  <property name="hibernate.connection.username">java</property>
                  <property name="hibernate.connection.SetBigStringTryClob">true</property>
                  <property name="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
                  <property name="hibernate.show_sql">true</property>
                  <property name="hibernate.format_sql">true</property>
                  <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
                  <mapping resource="com/study/database/hb/map/File.hbm.xml"/>
              </session-factory>
          </hibernate-configuration>

          File.hbm.xml文件

          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "
          http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
          <!-- Generated 2006-3-13 17:57:00 by Hibernate Tools 3.1.0.beta4 -->
          <hibernate-mapping>
              <class name="com.study.database.hb.map.File" table="StoreFile" batch-size="0">
                  <id name="uuid" type="string">
                      <column name="UUID" length="128" />
                      <generator class="uuid.hex"></generator>
                  </id>
                  <property name="name" type="string" lazy="false">
                      <column name="NAME" length="200"/>
                  </property>
                  <property name="file" type="string" lazy="false">
                      <column name="text" />
                  </property>
              </class>
          </hibernate-mapping>

          File.java文件

          package com.study.database.hb.map;
          // Generated 2006-3-13 17:57:00 by Hibernate Tools 3.1.0.beta4

          import java.sql.Clob;
          /**
           * File generated by hbm2java
           */

          public class File
              implements java.io.Serializable
          {
            // Fields

            private String uuid;
            private String name;
            private String file;
            // Constructors

            /** default constructor */
            public File()
            {
            }
            /** full constructor */
            public File(String name,String file)
            {
              this.name=name;
              this.file=file;
            }
            // Property accessors

            public String getUuid()
            {
              return this.uuid;
            }
            public void setUuid(String uuid)
            {
              this.uuid=uuid;
            }
            public String getName()
            {
              return this.name;
            }
            public void setName(String name)
            {
              this.name=name;
            }
            public String getFile()
            {
              return this.file;
            }
            public void setFile(String file)
            {
              this.file=file;
            }
          }
          //比以前特殊的地方是hibernate.cfg.xml多了個(gè)SetBigStringTryClob設(shè)置。

          //把clob映射成string類型,這樣在多過(guò)4000個(gè)字符時(shí)也不會(huì)出錯(cuò)了。操作方法和普通的string類型一樣。 


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 潞西市| 鹤庆县| 河西区| 永顺县| 麻城市| 昌黎县| 龙川县| 南溪县| 香港 | 马尔康县| 桑日县| 西乌珠穆沁旗| 田东县| 盈江县| 班玛县| 台中市| 克拉玛依市| 绥阳县| 浦县| 本溪| 湄潭县| 嘉禾县| 平果县| 安徽省| 七台河市| 阿合奇县| 宿迁市| 麦盖提县| 花莲市| 鲁山县| 玉树县| 宣恩县| 台中县| 当阳市| 香格里拉县| 微山县| 上思县| 阿克陶县| 房山区| 依兰县| 汨罗市|