Saving data into Clob using Hibernate in Oracle 10g

☆樱花仙子☆ 提交于 2019-12-22 00:36:06

问题


I have a table with a java.sql.Clob column in Hibernate

The hbm file:

<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:

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.

Not sure how to convert the String into java.sql.Clob?


回答1:


You need explicitly map the type of the field to java.sql.Clob.

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


来源:https://stackoverflow.com/questions/15367518/saving-data-into-clob-using-hibernate-in-oracle-10g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!