我在Hibernate
中有一个java.sql.Clob
列的表
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>
这是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;
}
我在休眠时试过这个:
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!!!");
我收到例外情况:
javax.sql.rowset.serial.SerialClob cannot be cast to oracle.sql.CLOB
上面提到的所有Clob
都是java.sql.Clob
类型。
不确定如何将String
转换为java.sql.Clob
?
答案 0 :(得分:4)
您需要将字段类型明确映射到java.sql.Clob
。
<property
name="data"
type="java.sql.Clob"
update="true"
insert="true"
column="data"
/>