求解决Hibernate操作clob问题

mengwenjun987 2011-11-15 09:59:42


package BlobLoginDao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.hibernate.Login.Login;

public class ClobLogin {
private Session session = null;
private Transaction tran = null;
private static final ThreadLocal <Session>s = new ThreadLocal();

public ClobLogin(){

Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
this.session = s.get();
if(session == null){

this.session = factory.openSession();
s.set(session);
}


}
public int saveclob(Login login,String description){

tran = this.session.beginTransaction();
login.setDescription(Hibernate.createClob(description));
session.save(login);
tran.commit();

return login.getId();
}
public String getClob(int id) throws SQLException, IOException{
Login login = (Login)session.get(Login.class, id);
Clob desc = (Clob)login.getDescription();

Reader rd = desc.getCharacterStream();//字符输出流
BufferedReader br = new BufferedReader(rd);
String sdesc = br.readLine();
return sdesc;



}
}
配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hibernate.Login.Login" table="LOGIN" schema="MWJ">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="22" scale="0" />
<generator class="increment">
<param name="sequence">S_ID</param>
</generator>
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="20" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="20" />
</property>
<property name="image" type="java.sql.Blob">
<column name="IMAGE" />
</property>
<property name="description" type="java.sql.Clob">
<column name="DESCRIPTION" />
</property>
</class>
</hibernate-mapping>
错误:

SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6, 1.5.7, 1.5.8]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select max(ID) from LOGIN
Hibernate: insert into MWJ.LOGIN (USERNAME, PASSWORD, IMAGE, DESCRIPTION, ID) values (?, ?, ?, ?, ?)
id=36
Exception in thread "main" java.sql.SQLException: could not reset reader
at org.hibernate.lob.ClobImpl.getCharacterStream(ClobImpl.java:100)
at org.hibernate.lob.SerializableClob.getCharacterStream(SerializableClob.java:64)
at BlobLoginDao.ClobLogin.getClob(ClobLogin.java:48)
at com.hibernate.test.Test.main(Test.java:93)
...全文
263 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧