说明:使用以下方法时首先确认你已经配置好了
如果运行报:org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
将Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();改 为:Session session = getHibernateTemplate().getSessionFactory().openSession();记得关闭session
以下内容转自http://hi.baidu.com/sunjsp/blog/item/9ca1fedb73c86e60d0164ec2.html
JAVA操作CLOB类型的数据库字段还是有点麻烦,虽然CLOB不像BLOB那样是用二进制来存值,但不能当成普通的String来操作,当初操作ORACLE这种字段类型时,可把我折腾了半天,现将如何操作的步骤写了个比较详细的例子和说明,以供大家参考.
这里只写最关键的几个地方,其它的地方平时该怎么写就怎么写
首先是POJO类:
import java.sql.Clob;
import java.util.Date;
/**
*
* @author coffee
*
*/
public class KybasicInfo implements java.io.Serializable {
private Long infoId;
private String infoTitle;
private String infoKeyword;
private Clob infoContent; //注意这个地方是java.sql.Clob类型的,生成默认是String类型的,需要手工改一下
private String infoContentToString; //这个是和infoContent做转换时,手工添加的一个,不和数据库里的字段进行对应的
private Long infoTop;
private String userSno;
private Date infoTime;
private Long icId;
private String infoStandby1;
private String infoStandby2;
private String infoStandby3;
private Long infoStandby4;
private Long infoStandby5;
private Long infoStandby6;
public KybasicInfo() {
}
public KybasicInfo(String infoTitle, String infoKeyword,
Clob infoContent, Long infoTop, String userSno,
Date infoTime, Long icId, String infoStandby1, String infoStandby2,
String infoStandby3, Long infoStandby4, Long infoStandby5,
Long infoStandby6) {
this.infoTitle = infoTitle;
this.infoKeyword = infoKeyword;
this.infoContent = infoContent;
this.infoTop = infoTop;
this.userSno = userSno;
this.infoTime = infoTime;
this.icId=icId;
this.infoStandby1 = infoStandby1;
this.infoStandby2 = infoStandby2;
this.infoStandby3 = infoStandby3;
this.infoStandby4 = infoStandby4;
this.infoStandby5 = infoStandby5;
this.infoStandby6 = infoStandby6;
}
public Long getInfoId() {
return this.infoId;
}
public void setInfoId(Long infoId) {
this.infoId = infoId;
}
public String getInfoTitle() {
return this.infoTitle;
}
public void setInfoTitle(String infoTitle) {
this.infoTitle = infoTitle;
}
public String getInfoKeyword() {
return this.infoKeyword;
}
public void setInfoKeyword(String infoKeyword) {
this.infoKeyword = infoKeyword;
}
public Clob getInfoContent() {
return this.infoContent;
}
public void setInfoContent(Clob infoContent) {
this.infoContent = infoContent;
}
public Long getInfoTop() {
return this.infoTop;
}
public void setInfoTop(Long infoTop) {
this.infoTop = infoTop;
}
public Date getInfoTime() {
return this.infoTime;
}
public void setInfoTime(Date infoTime) {
this.infoTime = infoTime;
}
public String getInfoStandby1() {
return this.infoStandby1;
}
public void setInfoStandby1(String infoStandby1) {
this.infoStandby1 = infoStandby1;
}
public String getInfoStandby2() {
return this.infoStandby2;
}
public void setInfoStandby2(String infoStandby2) {
this.infoStandby2 = infoStandby2;
}
public String getInfoStandby3() {
return this.infoStandby3;
}
public void setInfoStandby3(String infoStandby3) {
this.infoStandby3 = infoStandby3;
}
public Long getInfoStandby4() {
return this.infoStandby4;
}
public void setInfoStandby4(Long infoStandby4) {
this.infoStandby4 = infoStandby4;
}
public Long getInfoStandby5() {
return this.infoStandby5;
}
public void setInfoStandby5(Long infoStandby5) {
this.infoStandby5 = infoStandby5;
}
public Long getInfoStandby6() {
return this.infoStandby6;
}
public void setInfoStandby6(Long infoStandby6) {
this.infoStandby6 = infoStandby6;
}
public String getUserSno() {
return userSno;
}
public void setUserSno(String userSno) {
this.userSno = userSno;
}
public Long getIcId() {
return icId;
}
public void setIcId(Long icId) {
this.icId = icId;
}
public String getInfoContentToString() {
return infoContentToString;
}
public void setInfoContentToString(String infoContentToString) {
this.infoContentToString = infoContentToString;
}
}
hbm.xml配置文件
<?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">
<hibernate-mapping>
<class name="kymanage.kybasic.vo.KybasicInfo" table="KYBASIC_INFO" schema="BL">
<id name="infoId" type="java.lang.Long">
<column name="INFO_ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">INFO_ID</param>
</generator>
</id>
<property name="infoTitle" type="java.lang.String">
<column name="INFO_TITLE" length="100" />
</property>
<property name="infoKeyword" type="java.lang.String">
<column name="INFO_KEYWORD" length="100" />
</property>
<!--注意:这里要将默认生成的String类型改成java.sql.Clob-->
<property name="infoContent" type="java.sql.Clob">
<column name="INFO_CONTENT" />
</property>
<property name="infoTop" type="java.lang.Long">
<column name="INFO_TOP" precision="38" scale="0" />
</property>
<property name="userSno" type="java.lang.String">
<column name="USER_SNO" length="100" />
</property>
<property name="infoTime" type="java.util.Date">
<column name="INFO_TIME" length="7" />
</property>
<property name="icId" type="java.lang.Long">
<column name="IC_ID" precision="38" scale="0" />
</property>
<property name="infoStandby1" type="java.lang.String">
<column name="INFO_STANDBY1" length="100" />
</property>
<property name="infoStandby2" type="java.lang.String">
<column name="INFO_STANDBY2" length="100" />
</property>
<property name="infoStandby3" type="java.lang.String">
<column name="INFO_STANDBY3" length="100" />
</property>
<property name="infoStandby4" type="java.lang.Long">
<column name="INFO_STANDBY4" precision="38" scale="0" />
</property>
<property name="infoStandby5" type="java.lang.Long">
<column name="INFO_STANDBY5" precision="22" scale="0" />
</property>
<property name="infoStandby6" type="java.lang.Long">
<column name="INFO_STANDBY6" precision="22" scale="0" />
</property>
</class>
</hibernate-mapping>
hibernate DAO类:
import oracle.sql.CLOB;
import java.io.IOException;
import java.io.Writer;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.lob.SerializableClob;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* kybasicInfo
*
* @author coffee
*
*/
public class KybasicInfoDAOImpl extends HibernateDaoSupport {
protected void initDao() {
}
//新增,在action里将表单中的这个大文本按正常情况传过来,也就是传String类型过来,然后,再在DAO里做Clob类型转换,
//这里面可能会有其它层来传参,但最终还是将传过来的Stirng转换成Clob型的,然后通过文件流形式写入到CLOB字段中去
public void save(KybasicInfo kybasicInfo) {
try {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();
kybasicInfo.setInfoContent(Hibernate.createClob(" "));//注意,这里的参数是个空格,先新增一个空的Clob进去
session.save(kybasicInfo);
session.flush();//强制执行
session.refresh(kybasicInfo,LockMode.UPGRADE);
SerializableClob sc=(SerializableClob)kybasicInfo.getInfoContent();//kybasicInfo.getInfoContent()是Clob类型的
Clob wrapclob=sc.getWrappedClob();//这里的Clob是java.sql.Clob
CLOB clob=(CLOB)wrapclob;//这里的CLOB是oracle.sql.CLOB
Writer writer=clob.getCharacterOutputStream();
writer.write(kybasicInfo.getInfoContentToString());//kybasicInfo.getInfoContentToString() 是String类型的,在action里就是传这个进来,然后再通过文件流形式写成CLOB字段中
writer.close();
session.update(kybasicInfo);
tran.commit();
} catch (RuntimeException re) {
throw re;
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//更新,操作的新增是一样的,都是先接收String类型的参数过来,然后再将String的转成CLOB类型的
public void update(KybasicInfo persistentInstance) {
try {
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();
persistentInstance.setInfoContent(Hibernate.createClob(" "));
session.update(persistentInstance);
session.flush();
session.refresh(persistentInstance,LockMode.UPGRADE);
SerializableClob sc=(SerializableClob)persistentInstance.getInfoContent();
Clob wrapclob=sc.getWrappedClob();
CLOB clob=(CLOB)wrapclob;
Writer writer=clob.getCharacterOutputStream();
writer.write(persistentInstance.getInfoContentToString());
writer.close();
session.update(persistentInstance);
tran.commit();
} catch (RuntimeException re) {
throw re;
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取Clob字段值
public KybasicInfo findById(java.lang.Long id) {
try {
KybasicInfo instance = (KybasicInfo) getHibernateTemplate().get(
"kymanage.kybasic.vo.KybasicInfo", id);
Clob clob=instance.getInfoContent();//取得Clob的值
if(clob!=null){
String clobString="";
try {
clobString = clob.getSubString(1, (int)clob.length());//将Clob类型的值转换成String类型的值
instance.setInfoContentToString(clobString);//通过setter方法,设置String值,然后就可以通过instance.getInfoContentToString()来取值了
} catch (SQLException e) {
e.printStackTrace();
}
}
return instance;
} catch (RuntimeException re) {
throw re;
}
}
public static KybasicInfoDAOImpl getFromApplicationContext(
ApplicationContext ctx) {
return (KybasicInfoDAOImpl) ctx.getBean("KybasicInfoDAO");
}
}