贴几个自定义usertype

[code]
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};
}

}[/code]

[code]
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;
}

}


[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值