在 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
Saving data into Clob using Hibernate in Oracle 10g
提问by user182944
I have a table with a java.sql.Clob
column in Hibernate
我java.sql.Clob
在 Hibernate 中有一个带有列的表
The hbm
file:
该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 Clob
mentioned above are of type java.sql.Clob
.
Clob
上面提到的所有类型都是java.sql.Clob
。
Not sure how to convert the String
into java.sql.Clob
?
不知道如何转换String
成java.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"
/>