在Oracle 10g中使用Hibernate将数据保存到Clob中 [英] Saving data into Clob using Hibernate in Oracle 10g

查看:161
本文介绍了在Oracle 10g中使用Hibernate将数据保存到Clob中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate中有一个包含 java.sql.Clob 列的表

code> hbm file:

 < class name =com .model.ClobModeltable =table1> 
< id name =idcolumn =id>
< generator class =assigned>< / generator>
< / id>
< property name =clobDatatype =clob>
< column name =ClobData>< / column>
< / property>



这是 ClobModel

  private Integer id; 
public Integer getId(){
return id;
}

public void setId(Integer id){
this.id = id;
}

私人Clob clobData;

public Clob getClobData(){
return clobData;
}

public void setClobData(Clob clobData){
this.clobData = clobData;

当我在hibernate中试过时:

  SessionFactory sf = new Configuration().configure(clob.cfg.xml)。buildSessionFactory(); 
Session sess = sf.openSession();

ClobModel cb = new ClobModel();
cb.setId(101);
尝试{
// getClobData()方法返回String,尝试将其转换为java.sql.Clob,然后将其分配给模型
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不能转换为oracle.sql.CLOB 

上面提到的所有 Clob 都是类型 java.sql.Clob



不知道如何将 String 转换为 java.sql.Clob ?

解决方案

您需要显式地将字段类型映射到 java .sql.Clob

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


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

这篇关于在Oracle 10g中使用Hibernate将数据保存到Clob中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆