关于Clob类型在Hibernate中的应用
网上找的 留着以后忘了再看看
1.实体类 1
2.映射文件2
3.实体Form3
4.Hibernate的Helper3
5.Clob的插入5
6.Clob的修改5
7.Clob的读取6
8.Clob的删除 6项目中遇到要对Oralce数据库的CLOB字段进行处理的问题,即实现对CLOB值的插入,更新,查询。 Clob的的操作一般有三种方法:1。JDBC的方法。2:把clob当作String来处理。3.直接使用Clob。这里我介绍的是第三种方法。使用 Hibernate3 来管理数据库的连接session。
1.实体类Entity实休类PublicInfo.Java结构如下,contentString为辅助字段,不映射到数据库中,这里注意的是映射到hbm.xml中的content字段的属性设type=”clob”就行了。
import java.io.Serializable;
importjava.sql.Clob;
importjava.util.Date;
/**
*@authorZhangWenRui
*@hibernate.class
*/
publicclassPublicInfoimplementsSerializable{
privateLongid;
privateStringcontentString;
privateClobcontent;//内容
/**
*@hibernate.idgenerator-class="sequence"length="16"
*@hibernate.generator-paramname="sequence"value="seq_information"
*@return
*/
publicLonggetId(){
returnid;
}
publicvoidsetId(Longid){
this.id=id;
}
/**
*@hibernate.propertytype="clob"
*@return
*/
publicClobgetContent(){
returncontent;
}
publicvoidsetContent(Clobcontent){
this.content=content;
}
publicStringgetContentString(){
returncontentString;
}
}
2.映射文件
PublicInfo.hbm.xml映射文件如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="com.eclink.publicinfo.entity.PublicInfo"
table="publicInfo"
>
<id
name="id"
column="id"
type="java.lang.Long"
length="16"
>
<generatorclass="sequence">
<paramname="sequence">seq_information</param>
</generator>
</id>
<property
name="content"
type="clob"
update="true"
insert="true"
column="content"
/>
</class>
</hibernate-mapping>
3.实体Formimport org.apache.struts.validator.ValidatorForm;
/**
*@struts.formname="publicInfoForm"include-all="true"
*@authorZhangWenRui
*
*/
publicclassPublicInfoFormextendsValidatorForm{
privateStringcontentString;
publicStringgetContentString(){
returncontentString;
}
publicvoidsetContentString(StringcontentString){
this.contentString=contentString;
}
}4.Hibernate的Helper
hibernate.cfg.xml放置于/WEB-INF/hibernate.cfg.xml下。
HibernateUtils用于管理Hibernate的sessionfactory.
importorg.hibernate.Hibernate;
importorg.hibernate.HibernateException;
importorg.hibernate.SessionFactory;
importorg.hibernate.cfg.Configuration;
/**
*@authorZhangWenRui
*/public class HibernateUtils {
privatestaticSessionFactorysessionFactory;
staticConfigurationconfiguration=newConfiguration().configure("../hibernate.cfg.xml");
static{
try{
sessionFactory=configuration.buildSessionFactory();
}catch(Throwablee){
System.out.println("createsessionFactoryfailed!");
}
}
publicstaticSessionFactorygetSessionFactory(){
returnsessionFactory;
}
publicstaticvoidinitialize(Objectproxy){
if(proxy!=null){
try{
Hibernate.initialize(proxy);
}catch(HibernateExceptione){
thrownewRuntimeException("Can'tinitializehibernateproxy",e);
}
}
}
}
hibernate.cfg.xml内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
<session-factory>
<propertyname="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<propertyname="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<propertyname="hibernate.connection.url">jdbc:oracle:thin:@192.168.0.2:1521:info</property>
<propertyname="hibernate.connection.username">info</property>
<propertyname="hibernate.connection.password">info</property>
<propertyname="hibernate.connection.pool_size">10</property>
<propertyname="show_sql">true</property>
<propertyname="use_outer_join">true</property>
<propertyname="hibernate.jdbc.batch_size">20</property>
<mappingresource="com/publicinfo/entity/PublicInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>5.Clob的插入
public void addPublicInfo(PublicInfo publicInfo) throws SQLException, IOException {
org.hibernate.Sessionsession=HibernateUtils.getSessionFactory().openSession();//建立一个session
Transactiontx=session.beginTransaction();//事务
publicInfo.setContent(Hibernate.createClob(""));//插入空的clob值
session.save(publicInfo);
session.flush();
session.refresh(publicInfo,LockMode.UPGRADE);//锁定
org.hibernate.lob.SerializableClobcb=(org.hibernate.lob.SerializableClob)publicInfo.getContent();
java.sql.ClobwrapClob=(java.sql.Clob)cb.getWrappedClob();
if(wrapClobinstanceoforacle.sql.CLOB){
oracle.sql.CLOBclob=(oracle.sql.CLOB)wrapClob;
java.io.Writerwriter=clob.getCharacterOutputStream();
StringcontentStr=publicInfo.getContentString();
writer.write(contentStr);
writer.close();
}
tx.commit();
session.close();
}6.Clob的修改
publicvoidupdatePublicInfo(PublicInfopublicInfo)throwsSQLException,IOException{
Sessionsession=HibernateUtils.getSessionFactory().openSession();
Transactiontx=session.beginTransaction();
publicInfo.setContent(Hibernate.createClob(""));
session.saveOrUpdate(publicInfo);
session.flush();
session.refresh(publicInfo,LockMode.UPGRADE);
SerializableClobcb=(SerializableClob)publicInfo.getContent();
ClobwrapClob=(Clob)cb.getWrappedClob();
if(wrapClobinstanceofCLOB){
CLOBclob=(CLOB)wrapClob;
Writerwriter=clob.getCharacterOutputStream();
StringcontentStr=publicInfo.getContentString();
writer.write(contentStr);
writer.close();
}
tx.commit();
session.close();
}7.Clob的读取
Clob clob = publicInfo.getContent();
StringcontentStr="";
if(clob!=null)
contentStr = clob.getSubString(1, (int)clob.length());8.Clob的删除
由于删除操作不需要对Clob类型作特殊处理,只要通过id找出相应的实体删掉就行。