终于搞定关于Hibernte的Blob更新操作

11 篇文章 0 订阅

看了N多的文档,都是一样的,不理解,完全是网络转载,一点不负责任.所以我把我的完整代码贴如下:

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就不用写了吧!先看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;
    }


}
  方法的使用说明,我研究准确了,再附上!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
更新 Oracle 数据库中的 BLOB 字段,你可以使用以下步骤: 1. 首先,创建一个包含 BLOB 字段的数据表或在已有的表中添加 BLOB 字段。 ```sql CREATE TABLE my_table ( id NUMBER, blob_column BLOB ); ``` 2. 使用 UPDATE 语句更新 BLOB 字段的值。你可以使用 PL/SQL 块或 SQL 语句执行此操作。 使用 PL/SQL 块的示例: ```sql DECLARE l_blob BLOB; BEGIN -- 从文件中读取新的 BLOB 数据 SELECT empty_blob() INTO l_blob FROM dual; INSERT INTO my_table (id, blob_column) VALUES (1, l_blob); -- 打开 BLOB 对象以进行写入操作 DBMS_LOB.OPEN(l_blob, DBMS_LOB.LOB_READWRITE); -- 写入新的 BLOB 数据 DBMS_LOB.WRITE(l_blob, LENGTHB('New BLOB data'), 1, 'New BLOB data'); -- 关闭 BLOB 对象 DBMS_LOB.CLOSE(l_blob); -- 更新表中的记录 UPDATE my_table SET blob_column = l_blob WHERE id = 1; COMMIT; END; / ``` 使用 SQL 语句的示例: ```sql UPDATE my_table SET blob_column = ( SELECT empty_blob() FROM dual ) WHERE id = 1; DECLARE l_blob BLOB; BEGIN SELECT blob_column INTO l_blob FROM my_table WHERE id = 1 FOR UPDATE; -- 打开 BLOB 对象以进行写入操作 DBMS_LOB.OPEN(l_blob, DBMS_LOB.LOB_READWRITE); -- 写入新的 BLOB 数据 DBMS_LOB.WRITE(l_blob, LENGTHB('New BLOB data'), 1, 'New BLOB data'); -- 关闭 BLOB 对象 DBMS_LOB.CLOSE(l_blob); COMMIT; END; / ``` 请注意,这只是一个简单的示例,实际情况可能更复杂。你可能需要根据你的具体需求进行相应的调整。同时,确保在操作 BLOB 字段时进行适当的事务管理和异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值