简体   繁体   中英

Saving data into Clob using Hibernate in Oracle 10g

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM