简体   繁体   English

在Oracle 10g中使用Hibernate将数据保存到Clob中

[英]Saving data into Clob using Hibernate in Oracle 10g

I have a table with a java.sql.Clob column in Hibernate 我在Hibernate中有一个带有java.sql.Clob列的表

The hbm file: hbm文件:

<class name="com.model.ClobModel" table="table1">
   <id name="id" column="id">
      <generator class="assigned"></generator>
  </id> 
  <property name="clobData" type="clob">
      <column name="ClobData"></column>
  </property>

This is the ClobModel : 这是ClobModel

private Integer id;
public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

private Clob clobData;

public Clob getClobData() {
    return clobData;
}

public void setClobData(Clob clobData) {
    this.clobData = clobData;
}

When I tried this in hibernate: 当我在休眠中尝试这个时:

SessionFactory sf = new Configuration().configure("clob.cfg.xml").buildSessionFactory();
    Session sess = sf.openSession();

    ClobModel cb = new  ClobModel();
    cb.setId(101);
    try {
                // getClobData() method returns String, trying to convert it into java.sql.Clob and then assign it to the model
                   cb.setClobData(new javax.sql.rowset.serial.SerialClob(new ClobInsert().getClobData().toCharArray()));
    } catch (SerialException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    sess.save(cb);
    sess.flush();
    System.out.println("Exit!!!");

I am getting an exception: 我得到一个例外:

javax.sql.rowset.serial.SerialClob cannot be cast to oracle.sql.CLOB

All the Clob mentioned above are of type java.sql.Clob . 上面提到的所有Clob都是java.sql.Clob类型。

Not sure how to convert the String into java.sql.Clob ? 不确定如何将String转换为java.sql.Clob

You need explicitly map the type of the field to java.sql.Clob . 您需要将字段的类型显式映射到java.sql.Clob

<property
    name="data"
    type="java.sql.Clob"
    update="true"
    insert="true"
    column="data"
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jdbc 在 oracle 10g 中插入大 clob 数据(&gt; 4K 个字符) - How to insert Large clob data(>4K characters) in oracle 10g using jdbc Hibernate批量插入Oracle 10g - Hibernate batch insert oracle 10g 从 Oracle 10g 检索 CLOB 时出错 - Error while retieving CLOB from Oracle 10g 为什么使用带有Oracle 10g方言的Hibernate使用JPA创建名为hibernate_sequence的序列? - Why is a sequence named hibernate_sequence being created with JPA using Hibernate with the Oracle 10g dialect? 关于使用Oracle 10g进行后续锁定的Hibernate警告 - Hibernate warning about follow-on locking using Oracle 10g 如何使用带有Oracle 10g方言的Hibernate使用JPA生成我的id? - How is my id being generated with JPA using Hibernate with the Oracle 10g dialect? Oracle 10g Clob列中的特殊字符(UTF8字符)返回为? 或在Java中为空白 - Special Characters (UTF8 character) in Oracle 10g Clob Column returning as ? or blank in Java 如何使用Scriplet提高从Oracle 10g到jsp页面的数据传输速度 - how to increase the data transfer speed from oracle 10g into jsp pages using scriplet 哪个数据类型用于在Oracle 10g中存储DateTime? - Which data type is used to store DateTime in Oracle 10g? 使用Java swing与Oracle 10g创建JDBC连接 - creating JDBC connection with Oracle 10g using java swing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM