vjame

          優化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數據加載中……

          hibernate中對CLOB字段數據進行增刪修

           

          /*
           * Copyright: Jiang Xi Strong Co.. Ltd.
           * 
           * All right reserved.
           *  
           
          */
          package com.strongit.credit.manager.infomanager.dao.impl;

          import java.io.BufferedReader;
          import java.io.BufferedWriter;
          import java.io.IOException;
          import java.io.Serializable;
          import java.io.Writer;
          import java.lang.reflect.Method;
          import java.sql.Clob;
          import java.util.List;

          import oracle.sql.CLOB;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          import org.hibernate.Hibernate;
          import org.hibernate.LockMode;
          import org.hibernate.SQLQuery;
          import org.hibernate.Session;
          import org.hibernate.Transaction;
          import org.hibernate.lob.SerializableClob;

          import com.strongit.credit.common.dao.impl.CreditDAO;
          import com.strongit.credit.common.filter.ResultFilter;
          import com.strongit.credit.common.po.Info;
          import com.strongit.credit.common.po.Stat;
          import com.strongit.credit.common.po.Subject;
          import com.strongit.credit.manager.infomanager.dao.InfoDao;

          /*
           * Create Date:2008-9-22 上午09:54:46
           * 
           * Author:lanjh
           * 
           * Version: V1.0
           * 
           * Description:對進行修改的功能進行描述
           * 
           * 
           
          */

          public class InfoDaoImpl extends CreditDAO implements InfoDao {

              
          private Log log;

              
          public InfoDaoImpl() {
                  log 
          = LogFactory.getLog(InfoDaoImpl.class);
              }

              
          public void addip(Stat stat) {
                  
          this.getSession().save(stat);
              }

              
          public int count(ResultFilter resultfilter) {
                  
          // TODO Auto-generated method stub
                  return getResultCount(com.strongit.credit.common.po.InfoWrapper.class,
                          resultfilter);
              }

              
          public void delete(Info info) {
          //        this.getHibernateTemplate().delete(info);
                  Transaction tx = this.getSession().beginTransaction();
                  
          this.getSession().delete(info);
                  tx.commit();
              }

              
          public Info findInfoById(Long long1) {
                  
          return loadInfo(com.strongit.credit.common.po.InfoWrapper.class, long1);
              }

              
          public Subject findSubjectById(String s) {
                  
          return (Subject) this.getSession().get(
                          com.strongit.credit.common.po.Subject.
          class, s);
              }

              
          public List getInfos(ResultFilter resultfilter) {
                  
          return getResults(com.strongit.credit.common.po.InfoWrapper.class,
                          resultfilter);
              }

              
          public List getStat() {
                  String hql 
          = "";
                  hql 
          = "select sum(decode(t.comedate,null,0,1)) AS TOTAL, sum(decode(to_char(t.comedate,'yyyy-mm') ,to_char(ADD_MONTHS(SYSDATE,-1),'yyyy-mm'),1,0)) AS LASTMONTH, sum(decode(to_char(t.comedate,'yyyy-mm') ,to_char(ADD_MONTHS(SYSDATE,0),'yyyy-mm'),1,0)) AS THISMONTH, sum(decode(to_char(t.comedate,'yyyy-mm-dd') ,to_char(SYSDATE-1,'yyyy-mm-dd'),1,0)) AS YESTERDAY, sum(decode(to_char(t.comedate,'yyyy-mm-dd') ,to_char(SYSDATE,'yyyy-mm-dd'),1,0)) AS TODAY from STAT t ";
                  SQLQuery query 
          = this.getSession().createSQLQuery(hql);
                  
          return query.list();
              }

              
          public List getSubjects(ResultFilter resultfilter) {
                  
          return getResults(com.strongit.credit.common.po.Subject.class,
                          resultfilter);
              }

              
          public void insert(Info info) {

                  Session ssesion 
          = this.getSession();    
                  Transaction ts 
          = ssesion.beginTransaction();    

                  info.setContentClob(Hibernate.createClob(
          " "));    
                  ssesion.save(info);
                  ssesion.flush();
                  ssesion.refresh(info,LockMode.UPGRADE);    
                  SerializableClob clob
          =(SerializableClob)info.getContentClob();     
                  java.sql.Clob wrapClob 
          = clob.getWrappedClob();     
                  CLOB tmpClob
          =(CLOB)wrapClob;     
                  Writer wt;
                  
          try {
                      wt 
          = tmpClob.getCharacterOutputStream();
                      String content 
          = info.getContent();
                      wt.write(content);

                      wt.close();
                      ts.commit();
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
              }

              
          public void update(Info info) {
          //        Session ssesion = this.getSession();    
          //        Transaction ts = ssesion.beginTransaction();
          //        info = (Info)ssesion.load(Info.class,info.getId(),LockMode.UPGRADE); 
          //
          //        info.setContent(info.getContent());
          //        info.setContentClob(Hibernate.createClob(" "));
          //        ssesion.update(info);
          //        ssesion.flush();
          //        ssesion.refresh(info,LockMode.UPGRADE);
          //        SerializableClob clob=(SerializableClob)info.getContentClob();
          //        java.sql.Clob wrapClob = clob.getWrappedClob();     
          //        CLOB tmpClob=(CLOB)wrapClob;
          //        Writer wt;
          //        try {
          //            wt = tmpClob.setCharacterStream(0);
          //            String content = info.getContent();
          //            wt.write(content);
          //            wt.close();
          //            ts.commit();
          //        } catch (Exception e) {
          //            e.printStackTrace();
          //        }
                  this.delete(info);
                  
          this.insert(info);
              }
              
              
          public void saveOrUpdate(Info info)
              {
                  Session s;
                  BufferedWriter writer;
                  s 
          = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
                  writer 
          = null;
                  
          try
                  {
                      Class clazz 
          = Info.class;
                      Method m 
          = null;
                      String clobValue 
          = null;
                      clobValue 
          = info.getContent();
                      m 
          = clazz.getDeclaredMethod("setContentClob"new Class[] {
                          java.sql.Clob.
          class
                      });
                      m.setAccessible(
          true);
                      
          if(clobValue == null || clobValue.length() == 0)
                          m.invoke(info, 
          new Object[] {
                              Hibernate.createClob(
          "")
                          });
                      
          else
                          m.invoke(info, 
          new Object[] {
                              Hibernate.createClob(
          " ")
                          });

                      s.saveOrUpdate(info);
                      
          if(clobValue != null && clobValue.length() > 0)
                      {
                          s.flush();
                          s.refresh(info, LockMode.UPGRADE);
                          CLOB clob 
          = null;
                          m 
          = clazz.getDeclaredMethod("getContentClob"null);
                          m.setAccessible(
          true);
                          Object obj 
          = m.invoke(info, null);
                          
          if(obj instanceof SerializableClob)
                              clob 
          = (CLOB)((SerializableClob)obj).getWrappedClob();
                          
          if(clob != null)
                          {
                              writer 
          = new BufferedWriter(clob.getCharacterOutputStream());
                              writer.write(clobValue);
                          }
                      }
                  }
                  
          catch(Exception e)
                  {
                      StringBuilder sb 
          = new StringBuilder();
                      sb.append(
          "\u6301\u4E45\u5316POJO\u65F6\u51FA\u73B0\u9519\u8BEF\uFF0C\u7C7B ").append(info.getClass().getName()).append("\u3002").append(e.getMessage());
                      log.error(sb.toString());
                  }  
                  
                  
          if(writer != null)
                      
          try
                      {
                          writer.close();
                      }
                      
          catch(IOException e)
                      {
                          log.error(e.getMessage());
                      }
                  
          return;
              }


              
          public Info loadInfo(Class clazz, Serializable id) {
                  Object pojo;
                  Info info;
                  BufferedReader reader;
                  pojo 
          = this.getSession().get(clazz, id);
                  info 
          = (Info) pojo;
                  
          if (pojo == null || !(pojo instanceof Info)) {
                      
          this.getSession().refresh(pojo, LockMode.READ);
                  }
                  reader 
          = null;
                  
          try {
                      Class c 
          = com.strongit.credit.common.po.Info.class;
                      Method m 
          = c.getDeclaredMethod("getContentClob"null);
                      m.setAccessible(
          true);
                      Object result 
          = m.invoke(pojo, null);
                      
          if (result != null) {
                          reader 
          = new BufferedReader(((Clob) result)
                                  .getCharacterStream());
                          StringBuilder sb 
          = new StringBuilder();
                          
          for (String line = null; (line = reader.readLine()) != null;)
                              sb.append(line);

                          
          if (sb.length() > 0)
                              info.setContent(sb.toString());
                      }
                  } 
          catch (Exception e) {
                      StringBuilder sb 
          = new StringBuilder();
                      sb
                              .append(
                                      
          "\u52A0\u8F7DPOJO\u65F6\u51FA\u73B0\u9519\u8BEF\uFF0C\u7C7B ")
                              .append(clazz.getName()).append(
          "\u3002").append(
                                      e.getMessage());
                      log.error(sb.toString());
                  }

                  
          if (reader != null)
                      
          try {
                          reader.close();
                      } 
          catch (IOException e) {
                          log.error(e.getMessage());
                      }
                  
          return info;
              }

          }

          posted on 2008-09-25 08:41 lanjh 閱讀(966) 評論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 乌鲁木齐市| 庆安县| 大石桥市| 乌拉特前旗| 项城市| 郯城县| 荆门市| 新化县| 双流县| 隆安县| 喜德县| 普格县| 青冈县| 大名县| 蒲城县| 永年县| 饶平县| 湘潭县| 伽师县| 孟津县| 乌兰浩特市| 东海县| 堆龙德庆县| 海阳市| 乡城县| 东辽县| 电白县| 石城县| 灵川县| 富平县| 西乡县| 广灵县| 凌海市| 全南县| 衡阳市| 固始县| 平阴县| 莱州市| 西城区| 图片| 天等县|