【问题标题】:Reading Clob object using getCharacterStream API causes "java.sql.SQLException: could not reset reader"使用 getCharacterStream API 读取 Clob 对象会导致“java.sql.SQLException:无法重置阅读器”
【发布时间】:2012-10-28 23:08:30
【问题描述】:

我正在使用 Hibernate 3.6.10 版本并在保存记录后尝试读取 Clob 数据类型(学生)。它抛出错误“无法重置阅读器”

 public class Student implements java.io.Serializable {

    private long studentId;
    private String studentName;
    private Address studentAddress;
    private Clob searchProfileText;

在测试时...首先我保存学生记录,然后尝试再次从该记录中获取 searchProfileText,如下所示

1        student1.setSearchProfileText(clob);
2       session.save(student1);
3        System.out.println("Reading Clob : " + student1.getSearchProfileText().getCharacterStream());

第 3 行,出现以下异常

java.sql.SQLException: could not reset reader
at org.hibernate.engine.jdbc.ClobProxy.resetIfNeeded(ClobProxy.java:178)

我尝试了session.flush();,然后使用以下代码重新加载数据,仍然是同样的错误:

session.flush();
session.get(Student.class, student1.getStudentId());
System.out.println("Reading Clob : " + student1.getSearchProfileText().getCharacterStream());

观察 2:

即使我使用 Hibernate 条件获取包含 CLOB 数据的记录并对 CLOB 列设置限制,在获取记录后我也无法访问 CLOB 数据。我认为,这是 3.6.10 Final 中的一个 BUG !!!

请帮助摆脱这个错误。我已经尝试了所有相关主题,但还没有成功:(

【问题讨论】:

    标签: java hibernate clob


    【解决方案1】:

    由于您正在从same object instance 读取getSearchProfileText 对象,因此它无法这样做,因为指针在设置值后位于末尾。我认为您需要在阅读之前重新加载对象,例如

        Serializable  id = session.save(student1);
        //commit the transaction
        Student student = session.get(Student.class, id);
        System.out.println("Reading Clob :: " 
                               + student.getSearchProfileText().getCharacterStream());
    

    【讨论】:

    • 是的,你是对的。但是,我不能使用 flush() 来同步新的学生记录,然后重新加载相同的记录以再次读取 Clob 对象。 :( 你能帮忙吗?我如何在提交会话之前将其保存到休眠会话后读取相同的 Clob 对象。记住它适用于 Hibenrate 3.2.5,但不适用于 Hibernate 3.6.10。
    • @DhruboBhattacharjee:你是如何创建 CLOB 的?您不能在需要的地方使用相同的来源吗?
    • 使用 session.getLobHelper().createClob(String)... 创建 CLOB... 甚至在保存记录后我也无法访问相同的 clob 对象。指针没有休息到第一个位置。我目前正在使用 getSubString() API。让我知道是否有任何方法可以使用 getCharecterStream() API。
    • @DhruboBhattacharjee 由于您已经拥有用于创建Clob 的String,为什么不使用相同的字符串来获取流/阅读器,例如new InputStreamReader(new ByteArrayInputStream(string.getBytes()));?
    • 是的,这是一个选项,但是可能有很长的复杂方法层次结构,我不想每次都将字符串作为方法参数传递。我只是想知道为什么在刷新和重新加载记录后我无法访问 CLOB 数据!另外,即使你使用Hibernate条件和它对CLOB列的限制来获取包含CLOB数据的记录,在获取记录之后你也无法访问CLOB数据......这不是BUG吗?!!!
    猜你喜欢
    • 2018-07-05
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多