hibernate对oracle的clob操作

content.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="oracle.Content" table="Content">
<id name="code" column="code" type="string" length="20">
<generator class="assigned"/>
</id>
<property name="title" column="title" type="string" length="100"/>
<property name="annextitle" column="annextitle" type="string" length="100"/>
<property name="comefrom" column="comefrom" type="string" length="100"/>
<property name="author" column="author" type="string" length="100"/>
<property name="content" column="content" type="clob"  update="true" insert="true"  />
</class>
</hibernate-mapping>

content.java

package oracle;

import java.io.Serializable;
 
import oracle.sql.*;
import java.sql.Clob;

public class Content implements Serializable {

    /** identifier field */
    private String code;

    /** nullable persistent field */
    private String title;

    /** nullable persistent field */
    private String annextitle;

    /** nullable persistent field */
    private String comefrom;

    /** nullable persistent field */
    private String author;

    private Clob  content;

    /** default constructor */
    public Content() {
    }
    public  Clob getContent() {
           return this.content;
       }

           public void setContent( Clob content) {
                   this.content = content;
           }


    public  String getCode() {
        return this.code;
    }

        public void setCode( String code) {
                this.code = code;
        }

    public  String getTitle() {
        return this.title;
    }

        public void setTitle( String title) {
                this.title = title;
        }

    public  String getAnnextitle() {
        return this.annextitle;
    }

        public void setAnnextitle( String annextitle) {
                this.annextitle = annextitle;
        }

    public  String getComefrom() {
        return this.comefrom;
    }

        public void setComefrom( String comefrom) {
                this.comefrom = comefrom;
        }

    public  String getAuthor() {
        return this.author;
    }

        public void setAuthor( String author) {
                this.author = author;
        }

 

}
测试代码

test.java

package oracle;


import junit.framework.TestCase;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import oracle.sql.CLOB;

import java.io.Writer;

public class test extends TestCase
{
    private static SessionFactory _sessions = null;

    public void testCreate()
    {
        try
        {
            Configuration cfg = new Configuration().configure("/xhr.hbm.xml");
            SchemaExport dbExport = new SchemaExport(cfg);
            dbExport.create(true, true);

            _sessions = cfg.buildSessionFactory();
        } catch (MappingException e)
        {
            e.printStackTrace();
        } catch (HibernateException e)
        {
            e.printStackTrace();
        }
    }

    public void testAdd()
    {
        try
        {
            Configuration conf = new Configuration().configure("/xhr.hbm.xml");
            _sessions = conf.buildSessionFactory();
            Session hSessions = _sessions.openSession();
            Transaction tx = hSessions.beginTransaction();
            Content con = new Content();
            con.setCode("2004-12-20");
            con.setTitle("IBM.WEBSPHERE.APPLICATION.SERVER.V6.0");
            con.setAnnextitle("IBM.WEBSPHERE.APPLICATION.SERVER.V6.0");
            con.setAuthor("IBM");
            con.setComefrom("IBM");
            con.setContent(Hibernate.createClob("   "));
            hSessions.save(con);
            hSessions.flush();

            hSessions.refresh(con, LockMode.UPGRADE);
            CLOB clob = (CLOB) con.getContent();
            Writer out = clob.getCharacterOutputStream();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 1000; i++)
            {
                sb.append("那里有下载");
            }
            out.write(sb.toString());
            out.close();

            tx.commit();
            hSessions.close();

        } catch (Exception e)
        {
            e.printStackTrace();
        }


    }

}

 

xhr.hbm.xml

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
 <session-factory>
  <property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="connection.url">jdbc:oracle:thin:@192.168.13.4:1521:hong</property>
  <property name="connection.username">system</property>
  <property name="connection.password">manager</property>
 
    <mapping resource="oracle/Content.xml"/>
 
 
  </session-factory>

</hibernate-configuration>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值