论坛首页 入门技术论坛

贴几个自定义usertype

浏览 2554 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-02-24  
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import oracle.sql.BLOB;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

public class BinaryBlobType implements UserType {

	public Object deepCopy(Object value) throws HibernateException {
		if(value==null)return null;
		byte[] bytes=(byte[])value;
		byte[] result=new byte[bytes.length];
		System.arraycopy(bytes,0,result,0,bytes.length);
		return result;
	}

	public boolean equals(Object x, Object y) throws HibernateException {
		return (x == y)
				|| (x != null && y != null && java.util.Arrays.equals(
						(byte[]) x, (byte[]) y)); 
	}

	public boolean isMutable() {
		return true;
	}

	public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
		Blob blob=rs.getBlob(names[0]);
		return blob.getBytes(1,(int)blob.length());
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		
        if(st instanceof org.apache.commons.dbcp.DelegatingPreparedStatement && 
            ((org.apache.commons.dbcp.DelegatingPreparedStatement)st).getDelegate()     
               instanceof oracle.jdbc.OraclePreparedStatement)
          {
            oracle.sql.BLOB blob = oracle.sql.BLOB
                .createTemporary(
                    ((org.apache.commons.dbcp.PoolableConnection) st
                        .getConnection()).getDelegate(), false,
                    oracle.sql.BLOB.DURATION_SESSION);
                    
            blob.open(BLOB.MODE_READWRITE);
            
            OutputStream out = blob.getBinaryOutputStream();
                    
            try
            {
              out.write((byte[])value);
              out.flush();
              out.close();
            }
            catch(IOException e)
            {
              throw new SQLException("failed write to blob" + e.getMessage());
            }

            blob.close();
            ((oracle.jdbc.OraclePreparedStatement) ((org.apache.commons.dbcp.DelegatingPreparedStatement) st)
                .getDelegate()).setBLOB(index, blob);
          }
          else if(st instanceof oracle.jdbc.OraclePreparedStatement)
          {
              oracle.sql.BLOB blob =oracle.sql.BLOB.createTemporary(st.getConnection(),false,oracle.sql.BLOB.DURATION_SESSION);
              blob.open(BLOB.MODE_READWRITE);
              OutputStream out = blob.getBinaryOutputStream();
              try
              {
                  out.write((byte[]) value);
                  out.flush();
                  out.close();
              }
              catch (IOException e)
              {
                  throw new SQLException("failed write to blob" + e.getMessage());
              }
              blob.close();
             ((oracle.jdbc.OraclePreparedStatement)(st)).setBLOB(index,blob);
          }
          else
          {
            st.setBlob(index, Hibernate.createBlob((byte[]) value));
          } 
	}

	public Class returnedClass() {
		return Types.class;
	}

	public int[] sqlTypes() {
		return new int[]{Types.BLOB};
	}
	
}


import java.io.IOException;
import java.io.OutputStream;
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Types; 
import java.sql.Blob; 

import oracle.sql.BLOB;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

 
/**
* <li>Title: ORABinaryBlobType.java</li>
* <li>Project: Trunk</li>
* <li>Package: com.ideal.common.db.usertype</li>
* <li>Description: </li>
* <li>Copyright: Copyright (c) 2005</li>
* <li>Company: IdealTechnologies </li>
* <li>Created on Aug 24, 2006 12:36:01 PM</li>
* @author lei_chen
* @version 1.0
*/
public class ORABinaryBlobType implements UserType 
{ 
  public int[] sqlTypes() 
  { 
    return new int[] { Types.BLOB }; 
  }

  public Class returnedClass() 
  { 
    return byte[].class; 
  } 

  public boolean equals(Object x, Object y) 
  { 
    return (x == y) 
      || (x != null 
        && y != null 
        && java.util.Arrays.equals((byte[]) x, (byte[]) y)); 
  } 

  public Object nullSafeGet(ResultSet rs, String[] names, Object owner) 
  throws HibernateException, SQLException 
  { 
    Blob blob = rs.getBlob(names[0]); 
    return blob.getBytes(1, (int) blob.length()); 
  } 

  public void nullSafeSet(PreparedStatement st, Object value, int index) 
  throws HibernateException, SQLException 
  { 
      BLOB t_blob = BLOB.createTemporary(st.getConnection(),
          false, BLOB.DURATION_SESSION);
      OutputStream t_out = null;

      t_blob.open(BLOB.MODE_READWRITE);

      t_out = t_blob.getBinaryOutputStream();

      try
      {
        t_out.write((byte[]) value);
        t_out.flush();
        t_out.close();
      }
      catch (IOException e)
      {
        throw new SQLException("failed write to blob" + e.getMessage());
      }

      t_blob.close();

      st.setBlob(index, t_blob);
  } 

  public Object deepCopy(Object value) 
  { 
    if (value == null) return null; 

    byte[] bytes = (byte[]) value; 
    byte[] result = new byte[bytes.length]; 
    System.arraycopy(bytes, 0, result, 0, bytes.length); 

    return result; 
  } 

  public boolean isMutable() 
  { 
    return true; 
  } 

}


论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics