问题
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