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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15367518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:30:26  来源:igfitidea点击:

Saving data into Clob using Hibernate in Oracle 10g

javaoraclehibernateoracle10gclob

提问by user182944

I have a table with a java.sql.Clobcolumn in Hibernate

java.sql.Clob在 Hibernate 中有一个带有列的表

The hbmfile:

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 Clobmentioned above are of type java.sql.Clob.

Clob上面提到的所有类型都是java.sql.Clob

Not sure how to convert the Stringinto java.sql.Clob?

不知道如何转换Stringjava.sql.Clob

回答by Roman C

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"
/>