posts - 3,  comments - 2,  trackbacks - 0

          最近研究的重點(diǎn)內(nèi)容,先收集資料^.^
          轉(zhuǎn)自 http://blog.csdn.net/zengbo0710/archive/2007/03/30/1547099.aspx

          看了N多的文檔,都是一樣的,不理解,完全是網(wǎng)絡(luò)轉(zhuǎn)載,一點(diǎn)不負(fù)責(zé)任.所以我把我的完整代碼貼如下:

          sql: ============================================

          create table T_ZP
          (
            ID      NUMBER not null,
            RYBH    VARCHAR2(21),
            ZPXH    NUMBER,
            PIC     BLOB,
            OPTIME  DATE not null,
            DELFLAG CHAR(1) not null
          )
          ============================================

          bea和hbm.xml就不用寫(xiě)了吧!先看DAO方法:

           

          package com.sclh.hibernate.dao;

          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.sql.SQLException;
          import java.util.Iterator;
          import java.util.List;

          import oracle.sql.BLOB;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          import org.hibernate.Hibernate;
          import org.hibernate.HibernateException;
          import org.hibernate.LockMode;
          import org.hibernate.Query;
          import org.hibernate.Session;
          import org.hibernate.Transaction;
          import org.hibernate.lob.SerializableBlob;

          import com.sclh.common.GetTime;
          import com.sclh.hibernate.bean.Zp;

          /**
           * Data access object (DAO) for domain model class Zp.
           * 
           * 
          @see .Zp
           * 
          @author MyEclipse - Hibernate Tools
           
          */

          public class ZpDAO extends BaseHibernateDAO {

              
          private static final Log log = LogFactory.getLog(ZpDAO.class);

              
          public void delete(Zp persistentInstance) {
                  log.debug(
          "deleting Zp instance");
                  
          try {
                      getSession().delete(persistentInstance);
                      log.debug(
          "delete successful");
                  }
           catch (RuntimeException re) {
                      log.error(
          "delete failed", re);
                      
          throw re;
                  }

              }


              
          public boolean saveZp(Zp entity, String filePath) throws IOException {
                  log.debug(
          "saveZp " + entity.getClass().getName() + " instance");
                  
          boolean opFlag = false;
                  String idFlag 
          = "";
                  InputStream in 
          = null;
                  Session session 
          = null;
                  Transaction tx 
          = null;
                  Integer id 
          = null;
                  
          try {
                      session 
          = getSession();
                      Zp zp 
          = new Zp();
                      Zp zp1 
          = new Zp();
                      
          /* id的判斷 */
                      Query query 
          = session.createQuery("from Zp where delflag ='1'");
                      Iterator iterator 
          = query.iterate();
                      
          while (iterator.hasNext()) {
                          zp1 
          = (Zp) iterator.next();
                          System.out.println(
          "while ->照片Rybh:" + zp.getRybh());
                          
          if (entity.getRybh().equals(zp1.getRybh())
                                  
          && entity.getZpxh().equals(zp1.getZpxh())) {
                              idFlag 
          = "1";
                              System.out.println(
          "該人員有原始相片!");
                              update(entity, filePath);
                              
          break;
                          }

                      }

                      
          if (idFlag.equals("")) {
                          save(entity, filePath);
                      }

                  }
           finally {
                      session.close();
                  }

                  
          return opFlag;
              }


              
          public boolean save(Zp entity, String filePath) throws IOException {
                  
          boolean opFlag = false;
                  InputStream in 
          = null;
                  Session session 
          = null;
                  Transaction tx 
          = null;
                  Integer id 
          = null;
                  
          try {
                      session 
          = getSession();
                      tx 
          = session.beginTransaction();
                      Query query 
          = session.createQuery("select max(zp.id) from Zp zp");
                      List zpList 
          = query.list();
                      Iterator iterator 
          = query.iterate();

                      Integer integerId 
          = (Integer) iterator.next();
                      System.out.println(
          "id:" + integerId.toString());
                      
          int intId = (integerId.intValue()) + 1;
                      entity.setId(
          new Integer(intId));
                      System.out.println(
          "照片id:" + entity.getId());

                      entity.setRybh(entity.getRybh());
                      entity.setPic(Hibernate.createBlob(
          new byte[1]));
                      entity.setZpxh(entity.getZpxh());
                      entity.setOptime(GetTime.getSystemTime());
                      entity.setDelflag(
          "1");

                      session.save(entity);
                      session.flush();
                      session.refresh(entity, LockMode.UPGRADE);
                      SerializableBlob blob 
          = (SerializableBlob) entity.getPic();
                      java.sql.Blob wrapBlob 
          = blob.getWrappedBlob();
                      BLOB tmpBlob 
          = (BLOB) wrapBlob;

                      OutputStream out 
          = tmpBlob.getBinaryOutputStream();

                      FileInputStream image 
          = new FileInputStream(filePath);
                      
          byte[] buf = new byte[1024000];
                      
          int len = 0;
                      
          while ((len = image.read(buf)) > 0{
                          (out).write(buf, 
          0, len);
                      }

                      image.close();
                      out.close();

                      Zp zp2 
          = (Zp) session.load(Zp.class, entity.getId());
                      session.flush();
                      tx.commit();
                  }
           catch (HibernateException e) {
                      
          throw e;
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                      log.error(e.getMessage());
                  }
           finally {
                      
          if (tx != null{
                          tx.rollback();
                      }

                      session.close();
                  }


                  
          return opFlag;
              }


              
          public boolean update(Zp entity, String filePath) throws IOException {
                  
          boolean opFlag = false;
                  String idFlag 
          = "";
                  InputStream in 
          = null;
                  Session session 
          = null;
                  Transaction tx 
          = null;
                  Integer id 
          = null;
                  
          try {
                      session 
          = getSession();
                      tx 
          = session.beginTransaction();

                      Zp oldZp 
          = (Zp) session.load(Zp.class, entity.getId(),
                              LockMode.UPGRADE);
                      oracle.sql.BLOB blob_empty 
          = oracle.sql.BLOB.empty_lob();
                      oldZp.setPic(blob_empty);
                      session.flush();
                      session.refresh(oldZp, LockMode.UPGRADE);

                      oldZp.setId(
          new Integer(3));
                      oldZp.setRybh(entity.getRybh());
                      oldZp.setPic(Hibernate.createBlob(
          new byte[1]));
                      oldZp.setZpxh(entity.getZpxh());
                      oldZp.setOptime(GetTime.getSystemTime());
                      oldZp.setDelflag(
          "1");

                      session.update(oldZp);
                      session.flush();
                      session.refresh(oldZp, LockMode.UPGRADE);
                      SerializableBlob blob 
          = (SerializableBlob) oldZp.getPic();
                      java.sql.Blob wrapBlob 
          = blob.getWrappedBlob();
                      BLOB tmpBlob 
          = (BLOB) wrapBlob;

                      OutputStream out 
          = tmpBlob.getBinaryOutputStream();
                      FileInputStream image 
          = new FileInputStream(filePath);
                      
          byte[] buf = new byte[1024000];
                      
          int len = 0;
                      
          while ((len = image.read(buf)) > 0{
                          (out).write(buf, 
          0, len);
                      }

                      image.close();
                      out.close();

                      Zp zp2 
          = (Zp) session.load(Zp.class, oldZp.getId());
                      session.flush();
                      tx.commit();
                  }
           catch (HibernateException e) {
                      
          throw e;
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                      log.error(e.getMessage());
                  }
           finally {
                      
          if (tx != null{
                          tx.rollback();
                      }

                      session.close();
                  }


                  
          return opFlag;
              }


          }
          posted on 2007-07-31 00:51 Mr. Michael.Q 閱讀(351) 評(píng)論(0)  編輯  收藏 所屬分類: 站在他人的肩膀上
          主站蜘蛛池模板: 黄陵县| 焉耆| 三门峡市| 湖南省| 建阳市| 乌鲁木齐县| 梅河口市| 承德市| 沾益县| 泸西县| 唐海县| 全椒县| 桐城市| 江油市| 苍溪县| 黔江区| 长垣县| 会昌县| 田东县| 定边县| 兴宁市| 莲花县| 镇远县| 许昌县| 荆门市| 丽水市| 舞阳县| 兴城市| 阿克陶县| 丰原市| 栖霞市| 高唐县| 湟中县| 乳山市| 六枝特区| 宝应县| 宜黄县| 东方市| 和林格尔县| 申扎县| 普兰县|