欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

关于clob部类在hibernate中的应用

更新时间: 2014-05-17 13:33:55 责任编辑: Author_N7

 

关于Clob类型在Hibernate中的应用

网上找的 留着以后忘了再看看

 

 

 

1.实体类 1
2.映射文件 2
3.实体Form 3
4.Hibernate的Helper 3
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;
import java.sql.Clob;
import java.util.Date;
/**
* @author ZhangWenRui
* @hibernate.class
*/
public class PublicInfo implements Serializable{
private Long id;
   private String contentString;
   private Clob content;//内容
/**
     * @hibernate.id generator-class = "sequence" length = "16"
     * @hibernate.generator-param name="sequence" value="seq_information"
     * @return
     */
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
/**
     * @hibernate.property type="clob"
     * @return
     */
public Clob getContent() {
   return content;
}
public void setContent(Clob content) {
   this.content = content;
}
public String getContentString() {
   return contentString;
}
}
2.映射文件
PublicInfo.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="com.eclink.publicinfo.entity.PublicInfo"
        table="publicInfo"
    >
        <id
            name="id"
            column="id"
            type="java.lang.Long"
            length="16"
        >
            <generator class="sequence">
                <param name="sequence">seq_information</param>
            </generator>
        </id>
        <property
            name="content"
            type="clob"
            update="true"
            insert="true"
            column="content"
        />
    </class>
</hibernate-mapping>
3.实体Form

import org.apache.struts.validator.ValidatorForm;

/**
* @struts.form name="publicInfoForm" include-all = "true"
* @author ZhangWenRui
*
*/
public class PublicInfoForm extends ValidatorForm{
private String contentString;
public String getContentString() {
   return contentString;
}
public void setContentString(String contentString) {
   this.contentString = contentString;
}
}

4.Hibernate的Helper
hibernate.cfg.xml放置于/WEB-INF/hibernate.cfg.xml下。
HibernateUtils用于管理Hibernate的sessionfactory.
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* @author ZhangWenRui
*/

public class HibernateUtils {
private static SessionFactory sessionFactory;
static Configuration configuration = new Configuration().configure("../hibernate.cfg.xml");
static{
   try {
    sessionFactory = configuration.buildSessionFactory();
   } catch (Throwable e) {
    System.out.println("create sessionFactory failed!");
   }
}
public static SessionFactory getSessionFactory(){
   return sessionFactory;
}
public static void initialize(Object proxy) {
   if (proxy != null){  
   try {
    Hibernate.initialize(proxy);
   } catch (HibernateException e) {
    throw new RuntimeException("Can't initialize hibernate proxy", e);
   }
   }
}
}
hibernate.cfg.xml内容如下:
<?xml version="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>
   <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
   <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
   <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.0.2:1521:info</property>
   <property name="hibernate.connection.username">info</property>
   <property name="hibernate.connection.password">info</property>
   <property name="hibernate.connection.pool_size">10</property>
   <property name="show_sql">true</property>
   <property name="use_outer_join">true</property>
   <property name="hibernate.jdbc.batch_size">20</property>
  
   <mapping resource="com/publicinfo/entity/PublicInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>

5.Clob的插入

public void addPublicInfo(PublicInfo publicInfo) throws SQLException, IOException {
   org.hibernate.Session session = HibernateUtils.getSessionFactory().openSession();//建立一个session
   Transaction tx = session.beginTransaction();//事务
   publicInfo.setContent(Hibernate.createClob(" "));//插入空的clob值
   session.save(publicInfo);
   session.flush();
   session.refresh(publicInfo, LockMode.UPGRADE);//锁定
   org.hibernate.lob.SerializableClob cb = (org.hibernate.lob.SerializableClob) publicInfo.getContent();
   java.sql.Clob wrapClob = (java.sql.Clob) cb.getWrappedClob();
   if(wrapClob instanceof oracle.sql.CLOB){
    oracle.sql.CLOB clob = (oracle.sql.CLOB) wrapClob;
    java.io.Writer writer = clob.getCharacterOutputStream();
    String contentStr = publicInfo.getContentString();
    writer.write(contentStr);
    writer.close();
   }
   tx.commit();
   session.close();
}

 

6.Clob的修改
public void updatePublicInfo(PublicInfo publicInfo) throws SQLException, IOException {
   Session session = HibernateUtils.getSessionFactory().openSession();
   Transaction tx = session.beginTransaction();
   publicInfo.setContent(Hibernate.createClob(" "));
   session.saveOrUpdate(publicInfo);
   session.flush();
   session.refresh(publicInfo, LockMode.UPGRADE);
   SerializableClob cb = (SerializableClob) publicInfo.getContent();
   Clob wrapClob = (Clob) cb.getWrappedClob();
   if(wrapClob instanceof CLOB){
    CLOB clob = (CLOB) wrapClob;
    Writer writer = clob.getCharacterOutputStream();
    String contentStr = publicInfo.getContentString();
    writer.write(contentStr);
    writer.close();
   }
   tx.commit();
   session.close();
}

7.Clob的读取

Clob clob = publicInfo.getContent();
   String contentStr = "";
   if(clob != null)
    contentStr = clob.getSubString(1, (int)clob.length());

8.Clob的删除
由于删除操作不需要对Clob类型作特殊处理,只要通过id找出相应的实体删掉就行。

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-05-17 13:33:55
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/801007/
本文WWW.DOC100.NET DOC100.NET版权所有。